Thread interruption

David Bolen db3l at fitlinxx.com
Fri Apr 27 15:58:10 EDT 2001


Yann Cointepas <yann.cointepas at chups.jussieu.fr> writes:

> I made a program that is a graphical environment that is used to launch 
> user Python scripts. Each user script is executed in its own thread 
> "within" a common graphical environment provided by the main program. I 
> would like to allow the user to kill a script but I found no clean and 
> portable way to interrupt a thread from another thread.

You really need to find a way for the threads themselves to recognize
the need to be interrupted and to exit upon request from the
controlling environment.  Thus, interrupting one thread from another
becomes an issue of communicating the request to exit which the target
thread acts on at an appropriate time from its perspective.

Threads are not externally interruptable (in general) in Python, nor
in many underlying OSs (at least not without a high risk of subsequent
resource problems in the process).  It would be nice to have a
portable way to have interruption support on a per-thread basis, but I
wouldn't look for it anytime soon.  Note that I'm not intimately
familiar with pthreads, so someone more familiar with them might be
able to point out some way to bypass Python a bit and signal another
thread asynchronously.

However, it might not be as bad as you think - I'm not sure what each
of your user "scripts" are doing, but presumably the controlling
thread is watching the user scripts to be able to replicate feedback
to some presentation layer in the graphical environment for display,
correct?  If that's the case, you should be able to find some point in
the per-script threading that can look to see if a flag or other
sentinel has been set that it should exit and it can do so.  You may
have some latency problems if the per-script threads get blocked for
some reason for lengthy periods of time, depending on design.

--
-- David
-- 
/-----------------------------------------------------------------------\
 \               David Bolen            \   E-mail: db3l at fitlinxx.com  /
  |             FitLinxx, Inc.            \  Phone: (203) 708-5192    |
 /  860 Canal Street, Stamford, CT  06902   \  Fax: (203) 316-5150     \
\-----------------------------------------------------------------------/



More information about the Python-list mailing list