472,125 Members | 1,518 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,125 software developers and data experts.

Processes with strange behavoir

Hi.

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???
Thank you.

Best regards

Markus Franz

(some information about my computer: Python 2.3, SuSE Linux 9.0 Pro with
Kernel 2.4)

--------------------------------------------------
My script:
--------------------------------------------------

#!/usr/bin/python

import urllib2, sys, socket, os, string

# set timeout
socket.setdefaulttimeout(4)

# function for loading and printing a website
def myfunction(url):
try:
req = urllib2.Request(url)
req.add_header('Accept-Language', sys.argv[1])
req.add_header('User-Agent', 'Mozilla/4.0 (compatible; MSIE 6.0;
Windows NT 5.1)')
f = urllib2.urlopen(req)
contents = f.read()
output = "\n---PI001---" + sys.argv[2] + '---PI001---' + '---PI002-'
+ sys.argv[2] + '::' + f.geturl() + '::' + sys.argv[2] + "-PI002---\n" +
contents
print output
del output
f.close()
del contents
del f
del req
except:
pass

# start processes
for currenturl in sys.argv:
if currenturl != sys.argv[0] and currenturl != sys.argv[1] and
currenturl != sys.argv[2]:
PID = os.fork()
if PID == 0:
myfunction(currenturl)
exit
Jul 18 '05 #1
2 1349
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

Jul 18 '05 #2
"exit" -> doesn't do anything
"exit()" -> does something

Jeff

Jul 18 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

1 post views Thread by Markus Franz | last post: by
9 posts views Thread by JennaS | last post: by
2 posts views Thread by Eyvind W Axelsen | last post: by
24 posts views Thread by LineVoltageHalogen | last post: by
2 posts views Thread by Chris | last post: by
6 posts views Thread by Jean-Guillaume LALANNE | last post: by
1 post views Thread by Paguro Bernardo | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.