Embedding Python in a multi-threaded C++ application
Jeff Epler
jepler at unpythonic.net
Tue Nov 9 16:50:13 EST 2004
Something along the lines of PyErr_SetInterrupt() is the best you can
do in CPython, unless there's a facility I don't know about. If your
code is not running through the Python bytecode interpreter loop, then
it's in some unknown and uninterruptable state. I'm there may be
something else you can do, but it'll be a "you get to keep both pieces
[when it breaks]" kind of solution.
For some reasons why a facility to forcibly terminate a thread was
removed from Java, a language with a richer set of thread-related tools,
see
http://g.oswego.edu/dl/cpj/cancel.html
down at the heading "Asynchronous Termination":
The stop method was originally included in class Thread, but its
use has since been deprecated. Thread.stop causes a thread to
abruptly throw a ThreadDeath exception regardless of what it is
doing. (Like interrupt, stop does not abort waits for locks or IO.
But, unlike interrupt, it is not strictly guaranteed to abort wait,
sleep, or join.)
This can be an arbitrarily dangerous operation. Because Thread.stop
generates asynchronous signals, activities can be terminated while
they are in the midst of operations or code segments that
absolutely must roll back or roll forward for the sake of program
safety and object consistency.
The same considerations apply to Python, and additionally Python has
been designed on the side of simplicity and portability rather than to
offer all the features of some particular OS threading library.
(that said, PyErr_SetInterrupt *is* a lot like ThreadDeath, because it
can be fired at any moment the interpreter "between bytecodes", which
means all kinds of higher-level consistency requirements could be
violated)
Jeff
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 196 bytes
Desc: not available
URL: <http://mail.python.org/pipermail/python-list/attachments/20041109/9afe678b/attachment.sig>
More information about the Python-list
mailing list