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

PyThreadState_SetAsyncExc ignores except: clause

Hello!

I have an IVR server controlling Dialogic hardware through
a DLL exposing the C API to Python. The server farms out
incoming calls to python scripts based on the telephone
number that was dialled, and these scripts are written by
other people, sometimes in other companies, and quite often
these people have no idea that a hangup event is actually
quite hard to catch in the middle of a while1:pass. I had
all sorts of alive/dead variables. I had the server load
the code and pepper it with checks before I let it run.
Nothing worked for all cases, and 3 oclock mornings started
to get old (I use a live box, we have no test hardware ($$$)).

Now I ask single threads to store their thread_id, and
call PyThreadState_SetAsyncExc from my event loop. It
works very well. The only issue (aside from the fact
that I should not be using this at all ever ever ever
and that it was only put there for real developers),
is that the exception that is sent upwards from the C
call is not trapped (most?all?) of the time. I think
it's because I'm doing some braindead stuff, so I beg
someone to look at this for me....
the C function I use
---
static PyObject * cvoice_async_exc(PyObject *self, PyObject *args) {
PyObject *exc;
int id, n;
if (!PyArg_ParseTuple(args, "iO:async_exc", &id, &exc)) return NULL;
n = PyThreadState_SetAsyncExc(id, exc);
if (n > 1) {
PyThreadState_SetAsyncExc(id, NULL);
PyErr_SetString(ErrorObject,"Problem in async_exc: abort");
return NULL;
}
Py_INCREF(Py_None);
return Py_None;
}

and HangupError is defined like this:
---
class HangupError(Exception):
def __init__(self, msg=''):
self._msg = msg
Exception.__init__(self, msg)

so from Python I do this
---
dll.async_exc(s.thread_id, HangupError)
while the thread is doing this
---
500: try:
501: print "hello"
502: except HangupError:
503: pass
and I get an exception message (caught from an outer "except Exception,msg:")
that reads like this:

Traceback (most recent call last):
<snipsnip>
file starla.py, line 501, in run
print "hello"
HangupError
it was not caught?
I've quarter-solved the issue by doing this:

f = cStringIO.StringIO()
traceback.print_exc(None,f)
if f.getvalue().splitlines()[-1] == "HangupError": print "Caught"

any ideas or answers welcome.
b.
Jul 18 '05 #1
0 1132

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

Similar topics

13
by: KefX | last post by:
This may have been discussed before, but I'm kind of confused as to why Python doesn't support having both an except ~and~ a finally clause, like this: try: raise RuntimeException except:...
0
by: Antoon Pardon | last post by:
I have been playing a bit with PyThreadState_SetAsyncExc. Now the documentation abouth this call states the following: | Returns the number of thread states modified; if it returns a number |...
0
by: spam_john | last post by:
I am attempting to raise an exception in a thread currently calling PyEval_CallObject from another thread using PyThreadState_SetAsyncExc. The PyEval_CallObject is currently calling of a function...
0
by: gabriel.becedillas | last post by:
Does anyone know if PyThreadState_SetAsyncExc stops a thread while its inside a native extension ? I'm trying to stop a testing script that boils down to this: while True: print "aaa"...
0
by: gabriel.becedillas | last post by:
I use PyThreadState_SetAsyncExc to stop a python thread but there are situations when the thread doesn't stop and continues executing normally. After some debugging, I realized that the problem is...
20
by: John Salerno | last post by:
I'm starting out with this: try: if int(text) 0: return True else: self.error_message() return False except ValueError: self.error_message()
3
by: johan2sson | last post by:
The documentation for PyThreadState_SetAsyncExc says "To prevent naive misuse, you must write your own C extension to call this". Anyone care to list a few examples of such naive misuse? Johan
1
by: iwl | last post by:
what is the nThreadId-Parameter of PyThreadState_SetAsyncExc? I try to implement a Terminate-Button in my C-Prog for my embedded Python, but its hard to find an example how to stop an...
2
by: AWasilenko | last post by:
I can't figure out this problem Im having, I just can't understand why it is ignoring the call I put in. First the code (This is a cherrypy website): import sys, cherrypy, html class Root:...
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: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
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
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
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
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...
0
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...

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.