Passing Exceptions Across Threads

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Adam Mullins

    #1

    Passing Exceptions Across Threads

    Hello, I'm writing a physics simulator back-end with a built-in,
    threaded server that for the moment is quite simple. I've faced a few
    problems in writing this code, however, as it's the first time I've
    played with threading. For the moment, everything works decently, but I
    need (or rather, want) a method for passing caught exceptions in
    sub-threads to the main thread to be raised there. Although this
    solution isn't the only one, I feel it will be the most elegant (the
    thread/class will be called inside the main module within a try/except
    structure, which makes the program a bit more modular, in my opinion,
    and cleaner).

    Here is my code so far:


    You can see near the top where I clumsily tried to a hack a function
    into threading._Main Thread, in the hopes that if it were called from a
    sub-thread it would execute in the main thread. This is seemingly not
    so.

    Many thanks for any help.

  • aurelien.campeas@free.fr

    #2
    Re: Passing Exceptions Across Threads

    have you tried replacing :

    main = threading._Main Thread()

    with

    main = threading.curre ntThread() ?

    (not sure if that will be sufficient)

    Well your way to pass exception between threads looks like I would have
    done myself. But I am no expert.

    Have you considered using stackless Python ? It provides a much safer
    and efficient threading model (thinking about your physics simulator).
    Of course, if you want not to block on blocking calls (socket stuff),
    real threads are a way to go.

    Comment

    • robert

      #3
      Re: Passing Exceptions Across Threads

      Adam Mullins wrote:
      [color=blue]
      > Hello, I'm writing a physics simulator back-end with a built-in,
      > threaded server that for the moment is quite simple. I've faced a few
      > problems in writing this code, however, as it's the first time I've
      > played with threading. For the moment, everything works decently, but I
      > need (or rather, want) a method for passing caught exceptions in
      > sub-threads to the main thread to be raised there. Although this
      > solution isn't the only one, I feel it will be the most elegant (the
      > thread/class will be called inside the main module within a try/except
      > structure, which makes the program a bit more modular, in my opinion,
      > and cleaner).
      >
      > Here is my code so far:
      > http://rafb.net/paste/results/UESOWB24.html
      >
      > You can see near the top where I clumsily tried to a hack a function
      > into threading._Main Thread, in the hopes that if it were called from a
      > sub-thread it would execute in the main thread. This is seemingly not
      > so.
      >
      > Many thanks for any help.
      >[/color]

      The CallQueue and BackgroundCall. get_return() do that exception transfer
      already by default:




      -robert

      Comment

      Working...