summaryrefslogtreecommitdiffstats
path: root/Doc
diff options
context:
space:
mode:
authorBerker Peksag <berker.peksag@gmail.com>2016-01-21 21:59:49 (GMT)
committerBerker Peksag <berker.peksag@gmail.com>2016-01-21 21:59:49 (GMT)
commit7405c16533f30acf6c0b1f2035898666871449c3 (patch)
tree306048441e425d36a95605c77fe8062bb25fbb39 /Doc
parent1538b3d3dfff284c8976f19c5e475b99717bacd8 (diff)
downloadcpython-7405c16533f30acf6c0b1f2035898666871449c3.zip
cpython-7405c16533f30acf6c0b1f2035898666871449c3.tar.gz
cpython-7405c16533f30acf6c0b1f2035898666871449c3.tar.bz2
Issue #18620: Improve Pool examples in multiprocessing documentation
A single call to Pool.apply_async() will create only one process. To use all of the pool's processes, it should be invoked multiple times: with Pool(processes=4) as pool: results = [pool.apply_async(func, ()) for i in range(4)] Patch by Davin Potts.
Diffstat (limited to 'Doc')
-rw-r--r--Doc/library/multiprocessing.rst37
1 files changed, 26 insertions, 11 deletions
diff --git a/Doc/library/multiprocessing.rst b/Doc/library/multiprocessing.rst
index 5fdd0e2..8209ae9 100644
--- a/Doc/library/multiprocessing.rst
+++ b/Doc/library/multiprocessing.rst
@@ -361,8 +361,9 @@ processes in a few different ways.
For example::
- from multiprocessing import Pool
- from time import sleep
+ from multiprocessing import Pool, TimeoutError
+ import time
+ import os
def f(x):
return x*x
@@ -378,15 +379,29 @@ For example::
for i in pool.imap_unordered(f, range(10)):
print(i)
- # evaluate "f(10)" asynchronously
- res = pool.apply_async(f, [10])
- print(res.get(timeout=1)) # prints "100"
+ # evaluate "f(20)" asynchronously
+ res = pool.apply_async(f, (20,)) # runs in *only* one process
+ print(res.get(timeout=1)) # prints "400"
+
+ # evaluate "os.getpid()" asynchronously
+ res = pool.apply_async(os.getpid, ()) # runs in *only* one process
+ print(res.get(timeout=1)) # prints the PID of that process
+
+ # launching multiple evaluations asynchronously *may* use more processes
+ multiple_results = [pool.apply_async(os.getpid, ()) for i in range(4)]
+ print([res.get(timeout=1) for res in multiple_results])
+
+ # make a single worker sleep for 10 secs
+ res = pool.apply_async(time.sleep, (10,))
+ try:
+ print(res.get(timeout=1))
+ except TimeoutError:
+ print("We lacked patience and got a multiprocessing.TimeoutError")
- # make worker sleep for 10 secs
- res = pool.apply_async(sleep, [10])
- print(res.get(timeout=1)) # raises multiprocessing.TimeoutError
+ print("For the moment, the pool remains available for more work")
# exiting the 'with'-block has stopped the pool
+ print("Now the pool is closed and no longer available")
Note that the methods of a pool should only ever be used by the
process which created it.
@@ -2171,13 +2186,14 @@ with the :class:`Pool` class.
The following example demonstrates the use of a pool::
from multiprocessing import Pool
+ import time
def f(x):
return x*x
if __name__ == '__main__':
with Pool(processes=4) as pool: # start 4 worker processes
- result = pool.apply_async(f, (10,)) # evaluate "f(10)" asynchronously
+ result = pool.apply_async(f, (10,)) # evaluate "f(10)" asynchronously in a single process
print(result.get(timeout=1)) # prints "100" unless your computer is *very* slow
print(pool.map(f, range(10))) # prints "[0, 1, 4,..., 81]"
@@ -2187,9 +2203,8 @@ The following example demonstrates the use of a pool::
print(next(it)) # prints "1"
print(it.next(timeout=1)) # prints "4" unless your computer is *very* slow
- import time
result = pool.apply_async(time.sleep, (10,))
- print(result.get(timeout=1)) # raises TimeoutError
+ print(result.get(timeout=1)) # raises multiprocessing.TimeoutError
.. _multiprocessing-listeners-clients: