how to do non-blocking I/O

Donn Cave donn at u.washington.edu
Thu Mar 18 02:07:38 CET 2004


In article <mailman.105.1079557901.742.python-list at python.org>,
 "J. Xu" <xu at reflexsecurity.com> 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.

Far as I know, what you want to do may not be possible with
portable functions, so you may have to just implement separately
for each platform.

If you decide to use select or poll, note that they do not
account for data buffered in a file object.  This loses data,
in effect, because even though it's in your process buffer,
you won't read it if select says there's no new data.  You
can turn buffering off, but that forces readline() to read
1 byte at a time at an unreasonable cost in system calls.
In my opinion it makes much more sense to use recv on the
socket, and not use a file object here.

   Donn Cave, donn at u.washington.edu



More information about the Python-list mailing list