1 | n/a | import time |
---|
2 | n/a | import random |
---|
3 | n/a | |
---|
4 | n/a | from multiprocessing import Process, Queue, current_process, freeze_support |
---|
5 | n/a | |
---|
6 | n/a | # |
---|
7 | n/a | # Function run by worker processes |
---|
8 | n/a | # |
---|
9 | n/a | |
---|
10 | n/a | def worker(input, output): |
---|
11 | n/a | for func, args in iter(input.get, 'STOP'): |
---|
12 | n/a | result = calculate(func, args) |
---|
13 | n/a | output.put(result) |
---|
14 | n/a | |
---|
15 | n/a | # |
---|
16 | n/a | # Function used to calculate result |
---|
17 | n/a | # |
---|
18 | n/a | |
---|
19 | n/a | def calculate(func, args): |
---|
20 | n/a | result = func(*args) |
---|
21 | n/a | return '%s says that %s%s = %s' % \ |
---|
22 | n/a | (current_process().name, func.__name__, args, result) |
---|
23 | n/a | |
---|
24 | n/a | # |
---|
25 | n/a | # Functions referenced by tasks |
---|
26 | n/a | # |
---|
27 | n/a | |
---|
28 | n/a | def mul(a, b): |
---|
29 | n/a | time.sleep(0.5*random.random()) |
---|
30 | n/a | return a * b |
---|
31 | n/a | |
---|
32 | n/a | def plus(a, b): |
---|
33 | n/a | time.sleep(0.5*random.random()) |
---|
34 | n/a | return a + b |
---|
35 | n/a | |
---|
36 | n/a | # |
---|
37 | n/a | # |
---|
38 | n/a | # |
---|
39 | n/a | |
---|
40 | n/a | def test(): |
---|
41 | n/a | NUMBER_OF_PROCESSES = 4 |
---|
42 | n/a | TASKS1 = [(mul, (i, 7)) for i in range(20)] |
---|
43 | n/a | TASKS2 = [(plus, (i, 8)) for i in range(10)] |
---|
44 | n/a | |
---|
45 | n/a | # Create queues |
---|
46 | n/a | task_queue = Queue() |
---|
47 | n/a | done_queue = Queue() |
---|
48 | n/a | |
---|
49 | n/a | # Submit tasks |
---|
50 | n/a | for task in TASKS1: |
---|
51 | n/a | task_queue.put(task) |
---|
52 | n/a | |
---|
53 | n/a | # Start worker processes |
---|
54 | n/a | for i in range(NUMBER_OF_PROCESSES): |
---|
55 | n/a | Process(target=worker, args=(task_queue, done_queue)).start() |
---|
56 | n/a | |
---|
57 | n/a | # Get and print results |
---|
58 | n/a | print('Unordered results:') |
---|
59 | n/a | for i in range(len(TASKS1)): |
---|
60 | n/a | print('\t', done_queue.get()) |
---|
61 | n/a | |
---|
62 | n/a | # Add more tasks using `put()` |
---|
63 | n/a | for task in TASKS2: |
---|
64 | n/a | task_queue.put(task) |
---|
65 | n/a | |
---|
66 | n/a | # Get and print some more results |
---|
67 | n/a | for i in range(len(TASKS2)): |
---|
68 | n/a | print('\t', done_queue.get()) |
---|
69 | n/a | |
---|
70 | n/a | # Tell child processes to stop |
---|
71 | n/a | for i in range(NUMBER_OF_PROCESSES): |
---|
72 | n/a | task_queue.put('STOP') |
---|
73 | n/a | |
---|
74 | n/a | |
---|
75 | n/a | if __name__ == '__main__': |
---|
76 | n/a | freeze_support() |
---|
77 | n/a | test() |
---|