Notes on starting processes in Python
Using os.fork()
Here is an example using
os.fork()
to spawn 5 processes
each running the python function, myfunc. Don't forget
the os._exit() at the end. Per the docs, normally,
sys.exit() is used, but os._exit() can
be used in child processes after a fork. It does not call cleanup
handlers, flush stdio buffers, etc.
import os
import time
def myfunc(i):
print "sleeping 5 seconds from process %s" % i
time.sleep(5)
print "finished sleeping from process %s" % i
for i in range(5):
pid = os.fork()
if pid == 0:
myfunc(i)
os._exit(0)
Results:
sleeping 5 seconds from process 0 sleeping 5 seconds from process 1 sleeping 5 seconds from process 2 sleeping 5 seconds from process 3 sleeping 5 seconds from process 4
And 5 seconds later...
finished sleeping from process 0 finished sleeping from process 1 finished sleeping from process 2 finished sleeping from process 3 finished sleeping from process 4
Running an external script in subprocesses
Alternatively, if you want to run an external script in multiple processes,
you can use the Popen class in the
subprocess
module. For example, to run the following script, called "myscript.py":
"myscript.py"
import sys
import time
def myfunc(i):
print "sleeping 5 seconds from process %s" % i
time.sleep(5)
print "finished sleeping from process %s" % i
if __name__ == '__main__':
myfunc(sys.argv[1])
use the following Python code stored in the same directory:
"popen_ex.py"
from subprocess import Popen
for i in range(5):
Popen(['python', './myscript.py', str(i)])
The screen output is the same as the previous example. What's the differnce?
fork() copies the process memory space
including open file descriptors to the
child process. In the second example, since I am executing a new Python
interpreter from scratch, I get a "cleaner" start but probably more overhead as well.
Post a comment
About
I'm Eliot and this is my notepad for programming topics such as Python, Django, Ubuntu, Emacs, etc... more »
Search Blog
Tags
-
algorithms
(4)
-
aws
(8)
-
blogproject
(20)
-
c_cplusplus
(12)
-
cardstore
(8)
-
colinux
(2)
-
concurrency
(9)
-
conkeror
(2)
-
cygwin
(18)
-
datastructures
(15)
-
datetime
(3)
-
dell
(3)
-
django
(39)
-
emacs
(20)
-
files_directories
(10)
-
install_setup
(7)
-
javascript
(3)
-
keyboard
(6)
-
matplotlib
(5)
-
mercurial
(4)
-
nginx
(2)
-
preferences
(8)
-
processes
(3)
-
pyqt
(18)
-
python
(122)
-
ratpoison
(3)
-
regexes
(5)
-
rsync
(3)
-
softwaretools
(17)
-
sql
(13)
-
ssh
(7)
-
subversion
(6)
-
twisted
(6)
-
ubuntu
(60)
-
urxvt
(5)
-
vxworks
(25)
-
webservices
(4)
-
wmii
(7)
Blogroll
- Adam Gomaa
- Alex Clemesha
- Amir Salihefendic
- Armin Ronacher
- David Beazley
- David Ziegler
- Duncan McGreggor
- Gareth Rushgrave
- Glyph Lefkowitz
- Guido van Rossum
- Ian Bicking
- Jacob Kaplan-Moss
- James Bennett
- James Tauber
- Jesper Noehr
- Matt Harrison
- Nikolay Kolev
- Parand Darugar
- Peter Baumgartner
- Peter Bengtsson
- Rob Hudson
- Simon Willison
- Will McGugan