Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 2e15d60

Browse files
committed
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.
2 parents dce4ae8 + 7405c16 commit 2e15d60

1 file changed

Lines changed: 26 additions & 11 deletions

File tree

Doc/library/multiprocessing.rst

Lines changed: 26 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -361,8 +361,9 @@ processes in a few different ways.
361361

362362
For example::
363363

364-
from multiprocessing import Pool
365-
from time import sleep
364+
from multiprocessing import Pool, TimeoutError
365+
import time
366+
import os
366367

367368
def f(x):
368369
return x*x
@@ -378,15 +379,29 @@ For example::
378379
for i in pool.imap_unordered(f, range(10)):
379380
print(i)
380381

381-
# evaluate "f(10)" asynchronously
382-
res = pool.apply_async(f, [10])
383-
print(res.get(timeout=1)) # prints "100"
382+
# evaluate "f(20)" asynchronously
383+
res = pool.apply_async(f, (20,)) # runs in *only* one process
384+
print(res.get(timeout=1)) # prints "400"
385+
386+
# evaluate "os.getpid()" asynchronously
387+
res = pool.apply_async(os.getpid, ()) # runs in *only* one process
388+
print(res.get(timeout=1)) # prints the PID of that process
389+
390+
# launching multiple evaluations asynchronously *may* use more processes
391+
multiple_results = [pool.apply_async(os.getpid, ()) for i in range(4)]
392+
print([res.get(timeout=1) for res in multiple_results])
393+
394+
# make a single worker sleep for 10 secs
395+
res = pool.apply_async(time.sleep, (10,))
396+
try:
397+
print(res.get(timeout=1))
398+
except TimeoutError:
399+
print("We lacked patience and got a multiprocessing.TimeoutError")
384400

385-
# make worker sleep for 10 secs
386-
res = pool.apply_async(sleep, [10])
387-
print(res.get(timeout=1)) # raises multiprocessing.TimeoutError
401+
print("For the moment, the pool remains available for more work")
388402

389403
# exiting the 'with'-block has stopped the pool
404+
print("Now the pool is closed and no longer available")
390405

391406
Note that the methods of a pool should only ever be used by the
392407
process which created it.
@@ -2176,13 +2191,14 @@ with the :class:`Pool` class.
21762191
The following example demonstrates the use of a pool::
21772192

21782193
from multiprocessing import Pool
2194+
import time
21792195

21802196
def f(x):
21812197
return x*x
21822198

21832199
if __name__ == '__main__':
21842200
with Pool(processes=4) as pool: # start 4 worker processes
2185-
result = pool.apply_async(f, (10,)) # evaluate "f(10)" asynchronously
2201+
result = pool.apply_async(f, (10,)) # evaluate "f(10)" asynchronously in a single process
21862202
print(result.get(timeout=1)) # prints "100" unless your computer is *very* slow
21872203

21882204
print(pool.map(f, range(10))) # prints "[0, 1, 4,..., 81]"
@@ -2192,9 +2208,8 @@ The following example demonstrates the use of a pool::
21922208
print(next(it)) # prints "1"
21932209
print(it.next(timeout=1)) # prints "4" unless your computer is *very* slow
21942210

2195-
import time
21962211
result = pool.apply_async(time.sleep, (10,))
2197-
print(result.get(timeout=1)) # raises TimeoutError
2212+
print(result.get(timeout=1)) # raises multiprocessing.TimeoutError
21982213

21992214

22002215
.. _multiprocessing-listeners-clients:

0 commit comments

Comments
 (0)