473,386 Members | 1,764 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

how to do non-blocking I/O

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.
Jul 18 '05 #1
2 2579
In article <ma************************************@python.org >,
"J. Xu" <xu@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, do**@u.washington.edu
Jul 18 '05 #2
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
Jul 18 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: klaus triendl | last post by:
hi, recently i discovered a memory leak in our code; after some investigation i could reduce it to the following problem: return objects of functions are handled as temporary objects, hence...
25
by: Yves Glodt | last post by:
Hello, if I do this: for row in sqlsth: ________pkcolumns.append(row.strip()) ________etc without a prior:
32
by: Adrian Herscu | last post by:
Hi all, In which circumstances it is appropriate to declare methods as non-virtual? Thanx, Adrian.
8
by: Bern McCarty | last post by:
Is it at all possible to leverage mixed-mode assemblies from AppDomains other than the default AppDomain? Is there any means at all of doing this? Mixed-mode is incredibly convenient, but if I...
399
by: =?UTF-8?B?Ik1hcnRpbiB2LiBMw7Z3aXMi?= | last post by:
PEP 1 specifies that PEP authors need to collect feedback from the community. As the author of PEP 3131, I'd like to encourage comments to the PEP included below, either here (comp.lang.python), or...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.