Thread interruption

Donn Cave donn at u.washington.edu
Wed Jan 24 13:03:37 EST 2001


Quoth "David Allen" <s2mdalle at titan.vcu.edu>:
...
| My question is, how do I interrupt threads?  In each
| thread, there will be blocking system calls happening,
| and I need to be able to interrupt the threads very
| quickly.  This is not something where select() is
| the answer, because I'm worried about blocking
| from gethostbyname() and other socket related stuff
| too.

See last week's subject "threading: how to kill a (possibly blocked) thread?"

In summary, you can't.  Not in Python's thread support, and not
portably in any language.

Processes are a lot better than threads for some of this kind of
thing.  On one hand they are susceptible to the kind of control
you want to exert, and on the other hand they exit in a cleaner
and more reliable way when you do it.  For example, any file
descriptor opened by the subprocess will close, and resources
like network mbufs will get reclaimed.  It is not a drop-in
solution, but if you can do the work it can be more robust.

| On a completely unrelated note, I thought python
| threading was done within one process, and that
| the threading code had to deal with "timeslicing"
| between all the threads that actually belonged to
| one process.  But checking the output of 'ps' on
| running a threaded python program, actually 4-5
| instances of the interpreter seem to be running when
| I'm using threads.  What gives?

Could be a platform quirk.  Python doesn't implement its
own threads, and while your idea of how it would work sure
matches mine, I guess it's possible that "ps" could show
threads separately.  I know they do show up in the BeOS
ps, but specifically as threads that are executing within
the single Python process ("team").

	Donn Cave, donn at u.washington.edu



More information about the Python-list mailing list