Interrupting a thread...

Donn Cave donn at oz.net
Mon Dec 18 01:41:43 EST 2000


Quoth "David Allen" <s2mdalle at titan.vcu.edu>:
| I'm writing a program which does some networking,
| and I have a thread that is devoted to grabbing 
| things off of the network so that the program won't
| hang while it is reading data.
|
| The problem is, I also occasionally need to interrupt
| this data fetching operation, so I need a way to
| either raise an exception in the other thread, or
| otherwise signal to it to stop what it's doing.
| Since the download thread calls other functions
| that are not sensitive to the data in the original
| object, I can't check to see if some value has 
| changed each time I'm reading in a loop.
|
| Ideally, I'd like thread 1 to raise an exception
| that would be propagated in thread 2, not thread 1.
| Is this possible?  What other techniques might work?

No, and for me it would not be all that ideal either.
Exceptions don't interrupt anything, they're more of
a flow control mechanism.

I don't understand paragraph two, where functions
are not sensitive to the original object (what
original object?), so I'll guess you're asking a
question I have an answer for.  You actually have
a socket, and you want a thread to read data from
the socket as well as receive some other inputs.

The select() function allows you to wait for
events on multiple sockets/pipes.  In order to
take advantage of that, you need to create a
pipe or another socket, dedicated to communication
between the main thread and the socket thread.
The socket thread selects on the socket and the
read end of the pipe.  The main thread writes
to the pipe when it has something to say to the
socket thread.  That won't interrupt an actual
data transfer, but I don't think you really want
to do that.

	Donn Cave, donn at oz.net



More information about the Python-list mailing list