multithread exception handling

Discussion in 'Python' started by pegazik@gmail.com, Sep 19, 2005.

  1. Guest

    Hello.

    I have problem and I ask you for help. Probably there is some quite
    easy solution, but I can't see it.

    I'm trying to perform some action that have to be timeout safe. So here
    is the structure of my program:

    \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\
    def TimeoutHandler():
    print '!'
    raise Exception

    class Active:
    def Action:
    timer = Timer(1, TimeoutHandler)
    print '1'
    timer.start()
    try:
    print '2'
    time.sleep(20)
    print '3'
    except:
    print '4'
    else:
    print '5'
    timer.cancel()
    print '6'

    active = Actice()

    ///////////////////////////////////////////
    The output is:
    12!356

    My question is, why exception is not raised correctly? Could be the
    reason that (probably) timer is another thread and there is no
    exception in the main thread? How to solve this problem? [My timeout
    should be smaller than one second so I can't use signal.alarm()]

    Thanks for your reply,
    Krzysztof Nowak
     
    , Sep 19, 2005
    #1
    1. Advertising

  2. wrote:
    > Hello.
    >
    > I have problem and I ask you for help. Probably there is some quite
    > easy solution, but I can't see it.
    >
    > I'm trying to perform some action that have to be timeout safe. So here
    > is the structure of my program:
    >
    > \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\
    > def TimeoutHandler():
    > print '!'
    > raise Exception
    >
    > class Active:
    > def Action:
    > timer = Timer(1, TimeoutHandler)
    > print '1'
    > timer.start()
    > try:
    > print '2'
    > time.sleep(20)
    > print '3'
    > except:
    > print '4'
    > else:
    > print '5'
    > timer.cancel()
    > print '6'
    >
    > active = Actice()
    >
    > ///////////////////////////////////////////
    > The output is:
    > 12!356
    >
    > My question is, why exception is not raised correctly? Could be the
    > reason that (probably) timer is another thread and there is no
    > exception in the main thread? How to solve this problem? [My timeout
    > should be smaller than one second so I can't use signal.alarm()]
    >
    > Thanks for your reply,
    > Krzysztof Nowak
    >


    As far as I can see, the exception is raised in the CORRECT
    thread - the one (belonging to the Timer) that is executing a
    raise statement.

    If you want to raise an exception in a different thread, you have
    to arrange some inter-thread communication. Except that the way
    to communicate between threads is to share an object where one
    thread writes to an object's state and another waits for or
    periodically checks for a change in the objects state.

    It's difficult to say more without knowing what problem you are
    trying to solve.

    Steve
     
    Steve Horsley, Sep 19, 2005
    #2
    1. Advertising

  3. >>>>> (p) wrote:

    [Timer example snipped]
    >p> My question is, why exception is not raised correctly? Could be the
    >p> reason that (probably) timer is another thread and there is no
    >p> exception in the main thread?


    yes, the doc of Timer says it is a subclass of Thread, and therefore runs in
    a new thread.
    By the way, your program isn't even correct Python, so how can it give the
    output you have written down?

    When I run a similar (but corrct python) program, I do get an exception,
    but it is in another thread, and therefore not caught.
    You can post a signal to the main thread, however, to catch the exception:

    def TimeoutHandler():
    print '!'
    os.kill(0, signal.SIGUSR1)

    def handler():
    raise Exception

    class Active:
    def __init__(self):
    signal.signal(signal.SIGUSR1, handler)
    timer = Timer(1, TimeoutHandler)
    etc.
    --
    Piet van Oostrum <>
    URL: http://www.cs.uu.nl/~piet [PGP 8DAE142BE17999C4]
    Private email:
     
    Piet van Oostrum, Sep 20, 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. =?Utf-8?B?RWtlbXBk?=

    Need an advice with this (multithread)

    =?Utf-8?B?RWtlbXBk?=, Feb 1, 2005, in forum: ASP .Net
    Replies:
    5
    Views:
    448
    Steve C. Orr [MVP, MCSD]
    Feb 1, 2005
  2. steve
    Replies:
    1
    Views:
    3,835
    Larry Barowski
    Jun 15, 2004
  3. r_obert@REMOVE_THIS.hotmail.com

    Multithread or Multithread DLL?

    r_obert@REMOVE_THIS.hotmail.com, Nov 27, 2004, in forum: C++
    Replies:
    0
    Views:
    2,247
    r_obert@REMOVE_THIS.hotmail.com
    Nov 27, 2004
  4. Peter
    Replies:
    34
    Views:
    1,974
    James Kanze
    Oct 17, 2009
  5. VSK
    Replies:
    0
    Views:
    255
Loading...

Share This Page