Another socket/thread question

Donn Cave donn at u.washington.edu
Wed Mar 13 15:54:06 EST 2002


Quoth Cliff Wells <logiplexsoftware at earthlink.net>:
| On Wed, 13 Mar 2002 10:37:29 -0800
| Ken wrote:

|> I've got a thread that continously listens to a socket, and reports what it
|> receives. Blocking is turned on, and I would like it to stay that way since
|> the only thing the thread has to do, is listen to the socket.
|> 
|> My question is when the socket needs to be closed (program shuts down, or
|> that user is being disconnected by the server) is it possible to interupt a
|> socket.recv ()?
|> 
|> If not, whats the easiest way to cut processor time from looping endlessly
|> with blocking turned off?
|
| You might try using select() with a reasonable timeout period.  Then you
| won't busy loop, but you'll be able to check every few seconds to see if
| some condition is set that would indicate a program exit.
|
| Alternatively, you could have your shutdown function send something to the
| socket itself which would indicate to the thread that it should exit.  Or
| even some irrelevant data followed by a test on whether the thread should
| exit (the data would never be processed so what it is would be unimportant
| - it's used simply to cause socket.recv to return).

Or a new socket or pipe could be assigned for this purpose, since the
beauty of select is that it can monitor multiple descriptors.

	Donn Cave, donn at u.washington.edu



More information about the Python-list mailing list