how to do non-blocking I/O

Peter Hansen peter at engcorp.com
Wed Mar 17 21:02:46 EST 2004


J. Xu wrote:

> I am a newbie to python. I am writing a client application, I need to 
> read query results from the server. Mostly I'll have a file object for 
> the connection to the server. So I use read() or readline() to get the 
> response. But since these are blocking calls, I have to use a seperate 
> thread for the input reading. But then I have another problem, my input 
> thread running in the following loop
> 
> while not quit:
>    str = ins.readline()
>    .... process the response
> 
> so I can't terminate the thread when it's blocking in the readline() 
> until the connection is closed. So looks like I need to use non-blocking 
> IO such as select() or poll(). But since I need this application to be 
> cross-platform, and these are not well supported on Windows, I don't 
> know what to do.

select() is perfectly well supported (to a reasonable degree anyway) on 
Windows, at least with actual sockets.  Don't know whether it applies to 
file objects attached to sockets, and never had cause to check...

If you can use the sockets themselves, with .recv() instead, then you 
can safely use select() even on Windows.

-Peter



More information about the Python-list mailing list