Multiprocessing question

Steven D'Aprano steve+comp.lang.python at pearwood.info
Sun Jul 13 22:44:16 EDT 2014


On Sun, 13 Jul 2014 19:53:09 -0400, Paul LaFollette wrote:

> I have thrown together a little C/UNIX program that forks a child
> process, then proceeds to let the child and parent alternate.  Either
> can run until it pauses itself and wakes the other.
> 
> I would like to know if there be a way to create the same behavior in
> Python 3, preferably in a non-platform dependent fashion.

The most direct way of duplicating the Unix idiom of forking a child 
process is to use the os.fork function, which is (I believe) just a thin 
wrapper around the C fork function.

https://docs.python.org/3/library/os.html#os.fork


But this is not platform-independent, it is Unix only.

Alternatively, you can look at the various os.exec* functions, which are 
available on Windows and Unix, and see if any of them are useful.

But the best way to solve this in a platform independent way is to use 
one of the concurrency modules:

https://docs.python.org/3/library/concurrency.html

such as multiprocessing. Although these are written for Python 2 rather 
than 3, you may find them useful:

http://pymotw.com/2/multiprocessing/index.html#module-multiprocessing

https://www.ibm.com/developerworks/aix/library/au-multiprocessing/


-- 
Steven



More information about the Python-list mailing list