Processes with strange behavoir

Peter Otten __peter__ at web.de
Sun Apr 4 16:29:48 EDT 2004


Markus Franz wrote:

> Today I created a script called load.py for using at the command line
> written in Python 2.3.
> This script should load as many websites as given on the comand line and
> print them with a seperation string to stdout. The loading was done in
> parallel. (I used processes for this.)
> 
> The script was started by the following command:
> 
> ./load.py en 58746 http://www.python.com
> 
> Well, everything was fine. Then I wanted to load a second website and so I
> started the script with the following command:
> 
> ./load.py en 58746 http://www.python.com http://www.linux.org
> 
> The behaviour was strange: The last website (http://www.linux.org) was
> loaded and printed twice.
> 
> Then I started the script for a third time with the following command:
> 
> ./load.py en 58746 http://www.python.com http://www.linux.org
> http://www.suse.com
> 
> The result was: First websites was loaded and shown once, second website
> twice, and the third website was loaded and shown for four times!
> 
> (This behaviour occurs with ANY address given to the script...)
> 
> 
> Does anybody know an answer to my problem???

I think the structure of your script should be

import os, sys
for arg in sys.argv[1:]:
    pid = os.fork()
    if pid == 0:
        print arg # placeholder for the download routine
        break

As you have omitted the break statement, each child will complete the for
loop and thus continue to fork() for the remaining entries in sys.argv[1:].

Peter




More information about the Python-list mailing list