Processes with strange behavoir

Discussion in 'Python' started by Markus Franz, Apr 4, 2004.

  1. Markus Franz

    Markus Franz Guest

    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
     
    Markus Franz, Apr 4, 2004
    #1
    1. Advertising

  2. Markus Franz

    Jeff Epler Guest

    "exit" -> doesn't do anything
    "exit()" -> does something

    Jeff
     
    Jeff Epler, Apr 4, 2004
    #2
    1. Advertising

  3. Markus Franz

    Peter Otten Guest

    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
     
    Peter Otten, Apr 4, 2004
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Joachim Schmitz

    sscanf behavoir

    Joachim Schmitz, Apr 19, 2007, in forum: C Programming
    Replies:
    7
    Views:
    444
    Gunvant Patil
    Apr 20, 2007
  2. Replies:
    5
    Views:
    144
    YANAGAWA Kazuhisa
    Mar 8, 2006
  3. Sy Ys

    BackgrounDRb behavoir

    Sy Ys, May 27, 2007, in forum: Ruby
    Replies:
    1
    Views:
    111
    Ezra Zygmuntowicz
    May 27, 2007
  4. surf

    perl polymorphic behavoir ?

    surf, Feb 7, 2006, in forum: Perl Misc
    Replies:
    3
    Views:
    107
    A. Sinan Unur
    Feb 7, 2006
  5. Daniel Rucareanu

    Weird behavoir while using function aliases

    Daniel Rucareanu, Sep 19, 2006, in forum: Javascript
    Replies:
    22
    Views:
    217
    Richard Cornford
    Sep 25, 2006
Loading...

Share This Page