threading and signals - main thread solely responsible for signalhandling?

Discussion in 'Python' started by Maligree, Feb 13, 2010.

  1. Maligree

    Maligree Guest

    The main part of my script is a function that does many long reads
    (urlopen, it's looped). Since I'm hell-bent on employing SIGINFO to
    display some stats, I needed to run foo() as a seperate thread to
    avoid getting errno 4 (interrupted system call) errors (which occur if
    SIGINFO is received while urlopen is setting itself up/waiting for a
    response). This does the job, SIGINFO is handled without ever brutally
    interrupting urlopen.

    The problem is that after starting foo as a thread, my main thread has
    nothing left to do - unless it receives a signal, and I am forced to
    keep it in some sort of loop so that ANY signal handling can still
    occur. I thought I'd just occupy it with a simple while 1: pass loop
    but that, unfortunately, means 100% CPU usage.

    Is there any way I could put the main thread to sleep? Or perhaps my
    approach is totally wrong?
    Maligree, Feb 13, 2010
    #1
    1. Advertising

  2. Maligree

    MRAB Guest

    Maligree wrote:
    > The main part of my script is a function that does many long reads
    > (urlopen, it's looped). Since I'm hell-bent on employing SIGINFO to
    > display some stats, I needed to run foo() as a seperate thread to
    > avoid getting errno 4 (interrupted system call) errors (which occur if
    > SIGINFO is received while urlopen is setting itself up/waiting for a
    > response). This does the job, SIGINFO is handled without ever brutally
    > interrupting urlopen.
    >
    > The problem is that after starting foo as a thread, my main thread has
    > nothing left to do - unless it receives a signal, and I am forced to
    > keep it in some sort of loop so that ANY signal handling can still
    > occur. I thought I'd just occupy it with a simple while 1: pass loop
    > but that, unfortunately, means 100% CPU usage.
    >
    > Is there any way I could put the main thread to sleep? Or perhaps my
    > approach is totally wrong?
    >

    The simplest fix is to call time.sleep(seconds) in the loop. Repeated
    sleeps of 1 second, for example, consume very little CPU time.
    MRAB, Feb 13, 2010
    #2
    1. Advertising

  3. Maligree

    Guest

    On 04:43 pm, wrote:
    >The main part of my script is a function that does many long reads
    >(urlopen, it's looped). Since I'm hell-bent on employing SIGINFO to
    >display some stats, I needed to run foo() as a seperate thread to
    >avoid getting errno 4 (interrupted system call) errors (which occur if
    >SIGINFO is received while urlopen is setting itself up/waiting for a
    >response). This does the job, SIGINFO is handled without ever brutally
    >interrupting urlopen.
    >
    >The problem is that after starting foo as a thread, my main thread has
    >nothing left to do - unless it receives a signal, and I am forced to
    >keep it in some sort of loop so that ANY signal handling can still
    >occur. I thought I'd just occupy it with a simple while 1: pass loop
    >but that, unfortunately, means 100% CPU usage.
    >
    >Is there any way I could put the main thread to sleep? Or perhaps my
    >approach is totally wrong?


    I don't think those two options are mutually exclusive. ;)

    MRAB suggested you time.sleep() in a loop, which is probably fine.
    However, if you want to have even /less/ activity than that in the main
    thread, take a look at signal.pause().

    Also, perhaps not terribly interesting, signal.siginterrupt() was
    recently introduced, which will let you avoid EINTR if SIGINFO is
    received while urlopen is in a syscall (but will also prevent the signal
    from being handled until the syscall returns on its own).

    And there's always Twisted & friends. :)

    Jean-Paul
    , Feb 13, 2010
    #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. Xah Lee

    responsible license

    Xah Lee, Aug 31, 2003, in forum: C Programming
    Replies:
    13
    Views:
    540
    Rob Warnock
    Sep 6, 2003
  2. Lloyd Zusman
    Replies:
    0
    Views:
    293
    Lloyd Zusman
    Jan 11, 2007
  3. Daniel Lee

    Who is responsible creating client certificate?

    Daniel Lee, Jul 29, 2006, in forum: ASP .Net Web Services
    Replies:
    2
    Views:
    286
    Daniel Lee
    Aug 16, 2006
  4. Sari Connard
    Replies:
    2
    Views:
    118
    Andrew Thompson
    Oct 24, 2006
  5. Csaba

    Who's responsible for this ?

    Csaba, Sep 30, 2004, in forum: Perl Misc
    Replies:
    4
    Views:
    110
    Anno Siegel
    Oct 11, 2004
Loading...

Share This Page