fork() and anydbm question
Ralf Hildebrandt
hildeb at www.stahl.bau.tu-bs.de
Wed Feb 16 10:00:40 EST 2000
I've written a little program which mails me the daily Dilbert, Robotman etc.
It mainly consists of a function "fetch_and_mail()" which tries to get the
GIF, checks whether that particular GIF was already fetched yesterday (yes,
updates on those servers are not reliable!). This is done by writing the
GIF's md5 hash into a persistent dictionary using anydbm.
fetch_and_mail() calls fork() in order to parallelize the process of
fetching the GIF's:
def fetch_and_mail():
parent = os.fork() # return 0 in the child
if (parent > 0): # not 0, meaning parent process
time.sleep(5) # give the process 5s to fork
return # parent returns
if (parent == -1):
print "fork() failed"
sys.exit(1)
# we're in the child now
... lots of ugly code ...
try:
page = urllib.urlopen(mainurl)
except:
print "urlopen(" + mainurl + ") failed"
os._exit(1)
...
Questions:
* What about the database created by anydbm.open() ?
Does it become inconsistent since multiple processes write to it?
* Is the use of os._exit(1) in the child process correct? Or should I use
sys.exit(1) instead?
More information about the Python-list
mailing list