Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > How to get a raised exception from other thread

Reply
Thread Tools

How to get a raised exception from other thread

 
 
dcrespo
Guest
Posts: n/a
 
      10-14-2005
Hi all,

How can I get a raised exception from other thread that is in an
imported module?

For example:

---------------
programA.py
---------------

import programB

thread = programB.MakeThread()
thread.start()

---------------
programB.py
---------------
import threading, time

class SomeException(Exception):
pass

class MakeThread(threading.Thread):
def __init__(self):
threading.Thread.__init__(self)

def run(self):
i = 0
while 1:
print i
i += 1
time.sleep(1) #wait a second to continue
if i>10:
raise SomeException()


Thanks

Daniel

 
Reply With Quote
 
 
 
 
Jeremy Moles
Guest
Posts: n/a
 
      10-14-2005
On non-Windows system there are a ton of ways to do it--this is almost a
whole field unto itself. (D-BUS, fifos, sockets, shmfs, etc.) In
Windows, I wouldn't have a clue.

I guess this is a hard question to answer without a bit more
information.

On Fri, 2005-10-14 at 14:45 -0700, dcrespo wrote:
> Hi all,
>
> How can I get a raised exception from other thread that is in an
> imported module?
>
> For example:
>
> ---------------
> programA.py
> ---------------
>
> import programB
>
> thread = programB.MakeThread()
> thread.start()
>
> ---------------
> programB.py
> ---------------
> import threading, time
>
> class SomeException(Exception):
> pass
>
> class MakeThread(threading.Thread):
> def __init__(self):
> threading.Thread.__init__(self)
>
> def run(self):
> i = 0
> while 1:
> print i
> i += 1
> time.sleep(1) #wait a second to continue
> if i>10:
> raise SomeException()
>
>
> Thanks
>
> Daniel
>


 
Reply With Quote
 
 
 
 
dcrespo
Guest
Posts: n/a
 
      10-15-2005
Thanks for your answer, but I think that created thread in python
should create a thread either on windows and linux.

Can you give me Python example of how to do what I want to do? Thanks

 
Reply With Quote
 
Peter Hansen
Guest
Posts: n/a
 
      10-15-2005
dcrespo wrote:
> How can I get a raised exception from other thread that is in an
> imported module?


Define what "get" means for your purposes. It appears that you mean you
want to catch the exception, but in the thread which launched the other
thread in the first place. If that's true, please show where you would
expect to catch this exception, given that when you start the thread,
the main thread continues running and might even finish before the other
thread finishes.

> thread = programB.MakeThread()
> thread.start()

....
# later code which might have completed by the time the thread finishes
....

Are you looking, for example, for some kind of
thread.waitUntilCompletionAndReRaiseExceptions() method?

-Peter
 
Reply With Quote
 
themightydoyle@gmail.com
Guest
Posts: n/a
 
      10-16-2005
I also need an answer to this problem. I'm using windows. Throwing an
exception in thread B from thread A using a callback function.

The function runs, but the thread never actually catches the exception.
The try/except block is in the run() method (over-riden from the
Thread class)

 
Reply With Quote
 
Peter Hansen
Guest
Posts: n/a
 
      10-16-2005
wrote:
> I also need an answer to this problem.


What _is_ the problem? We're still waiting for a clear description from
the OP as to what the problem really is. How would you describe what
_your_ problem is?

> I'm using windows. Throwing an
> exception in thread B from thread A using a callback function.
>
> The function runs, but the thread never actually catches the exception.
> The try/except block is in the run() method (over-riden from the
> Thread class)


Rather than force us all to guess what you are doing wrong, maybe it
would be better if you posted a small amount of code that shows the problem.

Exceptions raised in threads can definitely be caught in the run()
method if the code is written correctly, so that is not in itself an issue.

-Peter
 
Reply With Quote
 
themightydoyle@gmail.com
Guest
Posts: n/a
 
      10-16-2005
Here's a dumbed down version of what i'm doing:

import time
import threading

class threader(threading.Thread):
def __init__(self):
threading.Thread.__init__(self)
pass

def run(self):
try:
while 1:
time.sleep(5)
except SystemExit:
print "Got Exit Message in thread"

def killMe(self):
raise SystemExit



thread1 = threader()
thread2 = threader()

thread1.start()
thread2.start()

time.sleep(5)

try:
print "Killing thread 1"
thread1.killMe()
print "killing thread 2"
thread2.killMe()
except SystemExit:
print "Got exceptin in main thread"



The exception is not propogated to the threads I spawned, but instead
comes back in the main thread.

 
Reply With Quote
 
themightydoyle@gmail.com
Guest
Posts: n/a
 
      10-16-2005
Nevermind. I found a better solution. I used shared memory to create
a keep-alive flag. I then use the select function with a specified
timeout, and recheck the keep-alive flag after each timeout.

Thanx for all the input.

 
Reply With Quote
 
Dennis Lee Bieber
Guest
Posts: n/a
 
      10-16-2005
On 15 Oct 2005 20:54:33 -0700, declaimed the
following in comp.lang.python:


> try:
> print "Killing thread 1"
> thread1.killMe()
> print "killing thread 2"
> thread2.killMe()
> except SystemExit:
> print "Got exceptin in main thread"
>
>
>
> The exception is not propogated to the threads I spawned, but instead
> comes back in the main thread.


No surprise there... The function invocation is being made in the
CONTEXT of the main thread...
--
> ================================================== ============ <
> | Wulfraed Dennis Lee Bieber KD6MOG <
> | Bestiaria Support Staff <
> ================================================== ============ <
> Home Page: <http://www.dm.net/~wulfraed/> <
> Overflow Page: <http://wlfraed.home.netcom.com/> <

 
Reply With Quote
 
Klaas
Guest
Posts: n/a
 
      10-16-2005
In article < .com>,
wrote:

> Nevermind. I found a better solution. I used shared memory to create
> a keep-alive flag. I then use the select function with a specified
> timeout, and recheck the keep-alive flag after each timeout.
>
> Thanx for all the input.


How about a threading.Event?

-Mike
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: How include a large array? Edward A. Falk C Programming 1 04-04-2013 08:07 PM
Exception in thread QueueFeederThread (most likely raised duringinterpreter shutdown) Ziliang Chen Python 3 02-22-2013 02:30 AM
Exception raised in wrong thread? pegazik Python 1 09-20-2005 01:08 PM
Exception feature creep! (was: re-entering in the normal flow after an exception is raised) Lonnie Princehouse Python 8 10-02-2004 09:16 PM
capture exception raised by child thread. Joe Wong Python 1 05-14-2004 11:50 AM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57