473,386 Members | 1,720 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

worker thread catching exceptions and putting them in queue

All,

in a worker thread setup that communicates via queues is it possible to
catch exceptions raised by the worker executed, put them in an object
and send them over the queue to another thread where the exception is
raised in that scope?

considering that an exception is an object I feel it ought to be
possible, however I do not see how to go about it.

does anyone have a pointer towards the solution?

Paul
Mar 5 '07 #1
5 3549
Paul Sijben wrote:
All,

in a worker thread setup that communicates via queues is it possible to
catch exceptions raised by the worker executed, put them in an object
and send them over the queue to another thread where the exception is
raised in that scope?

considering that an exception is an object I feel it ought to be
possible, however I do not see how to go about it.

does anyone have a pointer towards the solution?

Just raise the exception object found in the queue. Only make sure it _is_
an exception, as you can raise everything. So in your queue-putting-code
you might consider discriminating between the two cases, like this:
while True:
try:
result = 1, work()
except:
result = 2, sys.exc_info()[1]
queue.put(result)

-------
while True:
kind, result = queue.get()
if kind == 1:
do(result)
elif kind == 2:
raise result
Diez
Mar 5 '07 #2
Paul Sijben schrieb:
All,

in a worker thread setup that communicates via queues is it possible to
catch exceptions raised by the worker executed, put them in an object
and send them over the queue to another thread where the exception is
raised in that scope?

considering that an exception is an object I feel it ought to be
possible, however I do not see how to go about it.

does anyone have a pointer towards the solution?

Paul
You're right, even exceptions are objects in Python. For further
studies, read http://docs.python.org/lib/module-exceptions.html

You can catch an exception like this:
try:
worker.do_some_work_that_may_raise_an_exception()
except Exception, e:
# the first argument is the type of error you want to handle
# it is Exception here, the baseclass of all computation exceptions
# the second argument is the variable (name) where you want to save
# the specific exception raised to
# it's 'e' here, a common shortcut for exception
exception_handler.handle(e)
# notice that you can pass e around as you like

For further information on exceptions and how to handle them, read
chapter 8 of the tutorial, especially starting from 8.3:
http://docs.python.org/tut/node10.ht...00000000000000

P.S. I don't know if what I told still applies to Python 3.0 -- a lot of
changes are upcoming related to exception raising and handling.
Mar 5 '07 #3
Stargaming & Diez,

thanks very much!

Stargaming wrote:
Paul Sijben schrieb:
>All,

in a worker thread setup that communicates via queues is it possible to
catch exceptions raised by the worker executed, put them in an object
and send them over the queue to another thread where the exception is
raised in that scope?

considering that an exception is an object I feel it ought to be
possible, however I do not see how to go about it.

does anyone have a pointer towards the solution?

Paul

You're right, even exceptions are objects in Python. For further
studies, read http://docs.python.org/lib/module-exceptions.html

You can catch an exception like this:
try:
worker.do_some_work_that_may_raise_an_exception()
except Exception, e:
# the first argument is the type of error you want to handle
# it is Exception here, the baseclass of all computation exceptions
# the second argument is the variable (name) where you want to save
# the specific exception raised to
# it's 'e' here, a common shortcut for exception
exception_handler.handle(e)
# notice that you can pass e around as you like

For further information on exceptions and how to handle them, read
chapter 8 of the tutorial, especially starting from 8.3:
http://docs.python.org/tut/node10.ht...00000000000000

P.S. I don't know if what I told still applies to Python 3.0 -- a lot of
changes are upcoming related to exception raising and handling.
Mar 6 '07 #4
In article <45*********************@news.xs4all.nl>,
Paul Sijben <pa*********@xs4all.nlwrote:
>
in a worker thread setup that communicates via queues is it possible to
catch exceptions raised by the worker executed, put them in an object
and send them over the queue to another thread where the exception is
raised in that scope?

considering that an exception is an object I feel it ought to be
possible, however I do not see how to go about it.
One caution: because exceptions keep stack frames alive, you can have
garbage collection problems. Make sure to del the exception when you're
done with it.
--
Aahz (aa**@pythoncraft.com) <* http://www.pythoncraft.com/

"I disrespectfully agree." --SJM
Mar 8 '07 #5
En Thu, 08 Mar 2007 13:31:14 -0300, Aahz <aa**@pythoncraft.comescribió:
In article <45*********************@news.xs4all.nl>,
Paul Sijben <pa*********@xs4all.nlwrote:
>>
in a worker thread setup that communicates via queues is it possible to
catch exceptions raised by the worker executed, put them in an object
and send them over the queue to another thread where the exception is
raised in that scope?

One caution: because exceptions keep stack frames alive, you can have
garbage collection problems. Make sure to del the exception when you're
done with it.
Note that it's the traceback who keeps stack frames alive, not the
exception itself (at least on the current Python versions, might change in
the future). If no fancy processing of the traceback is needed, maybe
converting it to string (using the traceback module) before posting it to
the queue is enough.

--
Gabriel Genellina

Mar 8 '07 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

9
by: phil | last post by:
And sorry I got ticked, frustrating week >And I could help more, being fairly experienced with >threading issues and race conditions and such, but >as I tried to indicate in the first place,...
5
by: fooooo | last post by:
This is a network app, written in wxPython and the socket module. This is what I want to happen: GUI app starts. User clicks a button to 'start' the work of the app. When start is pressed, a new...
2
by: Mark Hoffman | last post by:
All, My application spawns a worker process by calling BeginInvoke with an asynchronous callback method. This callback method is responsible for calling EndInvoke. No problems there; pretty much...
8
by: emma middlebrook | last post by:
Hi My main UI thread needs to create a worker thread. To signal that the work is finished the UI should be updated. I'm worried about that worker thread calling back via a delegate when it...
6
by: James Radke | last post by:
Hello, I have a multithreaded windows NT service application (vb.net 2003) that I am working on (my first one), which reads a message queue and creates multiple threads to perform the processing...
5
by: Soren S. Jorgensen | last post by:
Hi, In my app I've got a worker thread (background) doing some calculations based upon user input. A new worker thread might be invoked before the previous worker thread has ended, and I wan't...
14
by: joey.powell | last post by:
I am using VS2005 for a windows forms application. I need to be able to use a worker thread function to offload some processing from the UI thread. The worker thread will need access to a...
6
by: sjoshi | last post by:
I'm able to use the Bacground Worker class to execute task and pain the UI. However I want to be able to execute n tasks in order, say n1, then n2 if there were no errors during n1, n3 so on. ...
3
by: Kevin | last post by:
Using this: http://msdn2.microsoft.com/en-us/library/3dasc8as(VS.80).aspx as an example I have a question concerning the reuse of objects. In the example 10 instances of the Fibonacci class...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.