473,320 Members | 1,953 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,320 software developers and data experts.

multithread exception handling

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 '05 #1
2 2183
pe*****@gmail.com 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
Sep 19 '05 #2
>>>>> pe*****@gmail.com (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 <pi**@cs.uu.nl>
URL: http://www.cs.uu.nl/~piet [PGP 8DAE142BE17999C4]
Private email: pi**@vanoostrum.org
Sep 20 '05 #3

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

Similar topics

11
by: adi | last post by:
Dear all, This is more like a theoretical or conceptual question: which is better, using exception or return code for a .NET component? I had created a COM object (using VB6), which uses...
0
by: r_obert | last post by:
Hello, I'm trying to create a worker thread for my VC++ program, and was wondering whether I should be linking with the Multithread /MT or Multithread DLL /MD option? I'm not quite sure, in...
3
by: Master of C++ | last post by:
Hi, I am an absolute newbie to Exception Handling, and I am trying to retrofit exception handling to a LOT of C++ code that I've written earlier. I am just looking for a bare-bones, low-tech...
4
by: zbcong | last post by:
Hello: I write a multithread c# socket server,it is a winform application,there is a richtextbox control and button,when the button is click,the server begin to listen the socket port,waiting for a...
2
by: zhebincong | last post by:
Hello: I write a multithread c# socket server,it is a winform application,there is a richtextbox control and button,when the button is click,the server begin to listen the socket port,waiting...
44
by: craig | last post by:
I am wondering if there are some best practices for determining a strategy for using try/catch blocks within an application. My current thoughts are: 1. The code the initiates any high-level...
0
by: Gordon Cone | last post by:
I am currently debugging a deadlock in a multithread C# application. It makes lots of calls to legacy unmanaged code. The application runs on windows sever 2003 and uses the sever version of the...
0
by: Gordon Cone | last post by:
I am currently debugging a deadlock in a multithread C# application. It makes lots of calls to legacy unmanaged code. The application runs on windows sever 2003 and uses the sever version of the...
1
by: George2 | last post by:
Hello everyone, Such code segment is used to check whether function call or exception- handling mechanism runs out of memory first (written by Bjarne), void perverted() { try{
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you

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.