473,796 Members | 2,537 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 3568
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(resul t)

-------
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_r aise_an_excepti on()
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_handl er.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_r aise_an_excepti on()
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_handl er.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*********@xs 4all.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**@pythoncra ft.com) <* http://www.pythoncraft.com/

"I disrespectfully agree." --SJM
Mar 8 '07 #5
En Thu, 08 Mar 2007 13:31:14 -0300, Aahz <aa**@pythoncra ft.comescribió:
In article <45************ *********@news. xs4all.nl>,
Paul Sijben <pa*********@xs 4all.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
2787
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, you've >provided next to no useful (IMHO) information to >let anyone help you more than this This is about 5% of the code. Uses no locks.
5
10677
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 thread is spawned (threading module) and this thread starts listening for data on a socket. When someone connects, a new thread is spawned, It needs to do I/O on that socket and open a GUI window so the user can communicate with the client...
2
3976
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 straight out of MSDN. My question is what happens when my worker thread raises an exception? I can easily catch the exception, but I've noticed that unless my async callback runs and EndInvoke gets called, then the worker thread never gets...
8
4523
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 has finished its work. UI stuff is message-queue based and handled by a single thread spinning the standard message loop. But the worker thread calling back is obviously a different thread and, of course, isn't going to be 'serialized' by the...
6
5000
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 for long running reports. When the processing is complete it uses crystal reports to load a template file, populate it, and then export it to a PDF. It works fine so far....
5
3549
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 only one worker thread running at any time (if a new worker thread start has been requested, any running worker thread results will be invalid). I'm using the below method to invoke a new worker thread, but when stress testing this I'm sometimes...
14
6891
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 datagridview on the form. I am using the following code to spawn the worker thread... Thread WorkerThread = new Thread(new ThreadStart(WT_MyFunction)); WorkerThread.IsBackground = true; WorkerThread.Start(); The problem I am having is...I cannot seem...
6
5964
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. I'm wondering what would be the best way to do this , do I wait in a do...while loop for each to finish and then start another ? thanks Sunit
3
2310
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 are made and then all put in the threadpool at once, which is well within the limits of the threadpool. However, what happens if you want to do 10,000 Fibonacci calculations
0
9683
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9529
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10231
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10176
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10013
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7550
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5443
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
4119
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2927
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.