show final results here # scalar=12: +6 parent, +6 in 6 children
showdata('parent end', scalar, vector) # array[i]=8: +2 parent, +6 in 6 children
The following is this script’s output on Windows. Trace through this and the code to
see how it runs; notice how the changed value of the global variable is not shared by
the spawned processes on Windows, but passed-in Value and Array objects are. The
final output line reflects changes made to shared memory in both the parent and
spawned children—the array’s final values are all 8.0, because they were incremented
twice in the parent, and once in each of six spawned children; the scalar value similarly
reflects changes made by both parent and child; but unlike for threads, the global is
per-process data on Windows:
C:\...\PP4E\System\Processes> multi3.py
parent start: pid:6204, global:0, value:0, array:[0.0, 0.0, 0.0]
child : pid:9660, global:0, value:0, array:[0.0, 0.0, 0.0]
loop1 (updates in parent, serial children)...
process 0 : pid:3900, global:0, value:1, array:[1.0, 0.0, 0.0]
process 1 : pid:5072, global:0, value:2, array:[1.0, 1.0, 0.0]
process 2 : pid:9472, global:0, value:3, array:[1.0, 1.0, 1.0]
loop2 (updates in parent, parallel children)...
process 1 : pid:9468, global:0, value:6, array:[2.0, 2.0, 2.0]
process 2 : pid:9036, global:0, value:6, array:[2.0, 2.0, 2.0]
process 0 : pid:9548, global:0, value:6, array:[2.0, 2.0, 2.0]
loop3 (updates in serial children)...
parent temp : pid:6204, global:6, value:9, array:[5.0, 5.0, 5.0]
loop4 (updates in parallel children)...
parent end : pid:6204, global:6, value:12, array:[8.0, 8.0, 8.0]
If you imagine the last test here run with a much larger array and many more parallel
children, you might begin to sense some of the power of this package for distributing
work.
Queues and subclassing
Finally, besides basic spawning and IPC tools, the multiprocessing module also:
- Allows its Process class to be subclassed to provide structure and state retention
(much like threading.Thread, but for processes). - Implements a process-safe Queue object which may be shared by any number of
processes for more general communication needs (much like queue.Queue, but for
processes).
Queues support a more flexible multiple client/server model. Example 5-32, for in-
stance, spawns three producer threads to post to a shared queue and repeatedly polls
for results to appear—in much the same fashion that a GUI might collect results in
parallel with the display itself, though here the concurrency is achieved with processes
instead of threads.
252 | Chapter 5: Parallel System Tools