Signals and system

Discussion in 'Python' started by T Koster, Apr 10, 2005.

  1. T Koster

    T Koster Guest

    Hi folks,

    My python program needs to download a number of files. Each file comes
    as a list of mirrors of that file.

    Currently, I am using system (os.system) to run wget. The mechanism is
    in a loop, so that it will try all the mirrors while wget is exiting
    with a non-zero exit status. This is working fine as long as the user
    feels there is no need to interrupt it.

    If wget receives a SIGINT, it stops (as expected) and returns non-zero
    (1 from memory). The call to system returns the same status code,
    indicating that wget failed, but the program has no knowledge that it
    was a signal the killed wget, rather than a failed download, and as such
    it tries the next mirror. I would like to be notified if wget received
    any signals, so that the user doesn't need to repetitively press Ctrl-C
    for each and every mirror to get the downloading process to stop.

    Can someone point me in the right direction?

    Thanks in advance,
    Koster
     
    T Koster, Apr 10, 2005
    #1
    1. Advertising

  2. # / 2005-04-10 20:55:05 +1000:
    > Hi folks,
    >
    > My python program needs to download a number of files. Each file comes
    > as a list of mirrors of that file.
    >
    > Currently, I am using system (os.system) to run wget. The mechanism is
    > in a loop, so that it will try all the mirrors while wget is exiting
    > with a non-zero exit status. This is working fine as long as the user
    > feels there is no need to interrupt it.
    >
    > If wget receives a SIGINT, it stops (as expected) and returns non-zero
    > (1 from memory). The call to system returns the same status code,
    > indicating that wget failed, but the program has no knowledge that it
    > was a signal the killed wget, rather than a failed download, and as such
    > it tries the next mirror. I would like to be notified if wget received
    > any signals, so that the user doesn't need to repetitively press Ctrl-C
    > for each and every mirror to get the downloading process to stop.
    >
    > Can someone point me in the right direction?


    http://docs.python.org/lib/os-process.html#l2h-1682

    "On Unix, the return value is the exit status of the process encoded
    in the format specified for wait()."

    http://docs.python.org/lib/os-process.html#l2h-1684

    "return a tuple containing its pid and exit status indication: a
    16-bit number, whose low byte is the signal number that killed the
    process, and whose high byte is the exit status (if the signal
    number is zero); the high bit of the low byte is set if a core file
    was produced"

    --
    How many Vietnam vets does it take to screw in a light bulb?
    You don't know, man. You don't KNOW.
    Cause you weren't THERE. http://bash.org/?255991
     
    Roman Neuhauser, Apr 10, 2005
    #2
    1. Advertising

  3. "T Koster" wrote:

    > Currently, I am using system (os.system) to run wget. The mechanism is
    > in a loop, so that it will try all the mirrors while wget is exiting
    > with a non-zero exit status. This is working fine as long as the user
    > feels there is no need to interrupt it.


    any reason you cannot use urllib2 (or urllib) with a socket timeout
    instead?

    something like:

    import socket, urllib

    list_of_mirrors = ...

    socket.setdefaulttimeout(10)

    for url in list_of_mirrors:
    try:
    f = urllib.urlopen(url)
    except IOError:
    print "trying another mirror"
    else:
    break

    # copy from the f stream to local file

    might work.

    </F>
     
    Fredrik Lundh, Apr 10, 2005
    #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. Martin Maurer
    Replies:
    2
    Views:
    2,105
    Charles Bailey
    May 21, 2004
  2. Taras_96
    Replies:
    5
    Views:
    2,169
    Taras_96
    Apr 14, 2005
  3. Jakub Moscicki
    Replies:
    2
    Views:
    551
    Jakub Moscicki
    Oct 4, 2003
  4. Murph
    Replies:
    9
    Views:
    470
    Mike Treseler
    Dec 7, 2006
  5. Viacheslav Chumushuk

    Catching system signals (UNIX).

    Viacheslav Chumushuk, Jan 15, 2009, in forum: Ruby
    Replies:
    1
    Views:
    115
    Michael Fellinger
    Jan 15, 2009
Loading...

Share This Page