cpython/Doc/includes/mp_workers.py

91 lines
2.0 KiB
Python
Raw Normal View History

#
# Simple example which uses a pool of workers to carry out some tasks.
#
# Notice that the results will probably not come out of the output
# queue in the same in the same order as the corresponding tasks were
# put on the input queue. If it is important to get the results back
# in the original order then consider using `Pool.map()` or
# `Pool.imap()` (which will save on the amount of code needed anyway).
#
Merged revisions 67398,67423-67424,67432,67440-67441,67444-67445,67454,67457,67463 via svnmerge from svn+ssh://pythondev@svn.python.org/python/trunk ........ r67398 | benjamin.peterson | 2008-11-26 18:39:17 +0100 (Wed, 26 Nov 2008) | 1 line fix typo in sqlite3 docs ........ r67423 | jesse.noller | 2008-11-28 19:59:35 +0100 (Fri, 28 Nov 2008) | 2 lines issue4238: bsd support for cpu_count ........ r67424 | christian.heimes | 2008-11-28 20:33:33 +0100 (Fri, 28 Nov 2008) | 1 line Retain copyright of processing examples. This was requested by a Debian maintainer during packaging of the multiprocessing package for 2.4/2.5 ........ r67432 | benjamin.peterson | 2008-11-29 00:18:46 +0100 (Sat, 29 Nov 2008) | 1 line SVN format 9 is the same it seems ........ r67440 | jeremy.hylton | 2008-11-29 00:42:59 +0100 (Sat, 29 Nov 2008) | 4 lines Move definition int sval into branch of ifdef where it is used. Otherwise, you get a warning about an undefined variable. ........ r67441 | jeremy.hylton | 2008-11-29 01:09:16 +0100 (Sat, 29 Nov 2008) | 2 lines Reflow long lines. ........ r67444 | amaury.forgeotdarc | 2008-11-29 03:03:32 +0100 (Sat, 29 Nov 2008) | 2 lines Fix a small typo in docstring ........ r67445 | benjamin.peterson | 2008-11-30 04:07:33 +0100 (Sun, 30 Nov 2008) | 1 line StringIO.close() stops you from using the buffer, too ........ r67454 | benjamin.peterson | 2008-11-30 15:43:23 +0100 (Sun, 30 Nov 2008) | 1 line note the version that works ........ r67457 | christian.heimes | 2008-11-30 22:16:28 +0100 (Sun, 30 Nov 2008) | 1 line w# requires Py_ssize_t ........ r67463 | skip.montanaro | 2008-12-01 02:55:22 +0100 (Mon, 01 Dec 2008) | 1 line typo in comment ........
2008-12-05 05:00:55 -04:00
# Copyright (c) 2006-2008, R Oudkerk
# All rights reserved.
#
import time
import random
from multiprocessing import Process, Queue, current_process, freeze_support
#
# Function run by worker processes
#
def worker(input, output):
for func, args in iter(input.get, 'STOP'):
result = calculate(func, args)
output.put(result)
#
# Function used to calculate result
#
def calculate(func, args):
result = func(*args)
return '%s says that %s%s = %s' % \
(current_process().name, func.__name__, args, result)
#
# Functions referenced by tasks
#
def mul(a, b):
time.sleep(0.5*random.random())
return a * b
def plus(a, b):
time.sleep(0.5*random.random())
return a + b
#
#
#
def test():
NUMBER_OF_PROCESSES = 4
TASKS1 = [(mul, (i, 7)) for i in range(20)]
TASKS2 = [(plus, (i, 8)) for i in range(10)]
# Create queues
task_queue = Queue()
done_queue = Queue()
# Submit tasks
for task in TASKS1:
task_queue.put(task)
# Start worker processes
for i in range(NUMBER_OF_PROCESSES):
Process(target=worker, args=(task_queue, done_queue)).start()
# Get and print results
print 'Unordered results:'
for i in range(len(TASKS1)):
print '\t', done_queue.get()
# Add more tasks using `put()`
for task in TASKS2:
task_queue.put(task)
# Get and print some more results
for i in range(len(TASKS2)):
print '\t', done_queue.get()
# Tell child processes to stop
for i in range(NUMBER_OF_PROCESSES):
task_queue.put('STOP')
if __name__ == '__main__':
freeze_support()
test()