It is, however, possible to start new programs in the child processes spawned, using
tools like the os.exec* calls we met earlier—by spawning a process portably with
multiprocessing and overlaying it with a new program this way, we start a new inde-
pendent program, and effectively work around the lack of the os.fork call in standard
Windows Python.
This generally assumes that the new program doesn’t require any resources passed in
by the Process API, of course (once a new program starts, it erases that which was
running), but it offers a portable equivalent to the fork/exec combination on Unix.
Furthermore, programs started this way can still make use of more traditional IPC tools,
such as sockets and fifos, we met earlier in this chapter. Example 5-33 illustrates the
technique.
Example 5-33. PP4E\System\Processes\multi5.py
"Use multiprocessing to start independent programs, os.fork or not"
import os
from multiprocessing import Process
def runprogram(arg):
os.execlp('python', 'python', 'child.py', str(arg))
if name == 'main':
for i in range(5):
Process(target=runprogram, args=(i,)).start()
print('parent exit')
This script starts 5 instances of the child.py script we wrote in Example 5-4 as inde-
pendent processes, without waiting for them to finish. Here’s this script at work on
Windows, after deleting a superfluous system prompt that shows up arbitrarily in the
middle of its output (it runs the same on Cygwin, but the output is not interleaved
there):
C:\...\PP4E\System\Processes> type child.py
import os, sys
print('Hello from child', os.getpid(), sys.argv[1])
C:\...\PP4E\System\Processes> multi5.py
parent exit
Hello from child 9844 2
Hello from child 8696 4
Hello from child 1840 0
Hello from child 6724 1
Hello from child 9368 3
This technique isn’t possible with threads, because all threads run in the same process;
overlaying it with a new program would kill all its threads. Though this is unlikely to
be as fast as a fork/exec combination on Unix, it at least provides similar and portable
functionality on Windows when required.
The multiprocessing Module| 255