473,385 Members | 1,357 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,385 software developers and data experts.

Non-blocking input on windows, like select in Unix

Hello

I have written a program that interacts with a
command line program.

Roughly speaking, it mimics human interaction.
(With more speed and accuracy, less intelligence.)
It works fine under Linux, using select().

But Windows does not support select for files.
Only for sockets.

Here's a google search on this topic.

<http://groups.google.co.uk/groups?q=select+windows+non-blocking&hl=en&lr=&group=comp.lang.python&scoring= d&start=0&sa=N>
I'd like my program to work under Windows also.

I'm exploring the following approach.
stdin, stdout = os.popen2('tex')

Now read a byte at a time from stdout.

Whenever a prompt string from TeX appears,
write the response to stdin.

An experiment at the Python command line shows
that this works. At each step either
a) we can read one byte from stdout w/o blocking
or
b) we can usefully write to stdin.

By the way, this experiment is rather tedious.
Still, I hope to have my computer do this for me.
So, unless I'm mistaken, I can get the program to
work under Windows. Though reading a byte at a
time might give a performance penalty. Can't say
yet on that.

My question is this: Under Windows, is it possible
to read as many bytes as are available from stdout,
without blocking?

This is one way to improve performance. (If needed.)

Another way, is to use _longer_ prompt strings.

If prompt strings are at least 64 bytes long, then
we can safely read 64 bytes -- unless we are in
the process of reading what might be a prompt
string.

This will of course increase performance in the
limiting case of when there are zero prompt
strings, and expensive system calls.
This problem of non-blocking input on Windows seems
to arise often. I hope my remarks might be helpful
to others. Certainly, I've found it helpful to
write them.
--
Jonathan
http://www.pytex.org

Jul 18 '05 #1
4 5891
Jonathan Fine <jf***@pytex.org> writes:
My question is this: Under Windows, is it possible
to read as many bytes as are available from stdout,
without blocking?


I think Windows implements non-blocking i/o calls. However the
traditional (to some) Python or Java approach to this problem is
to use separate threads for the reader and writer, and let them block
as needed.
Jul 18 '05 #2
Paul Rubin wrote:
Jonathan Fine <jf***@pytex.org> writes:
My question is this: Under Windows, is it possible
to read as many bytes as are available from stdout,
without blocking?

I think Windows implements non-blocking i/o calls. However the
traditional (to some) Python or Java approach to this problem is
to use separate threads for the reader and writer, and let them block
as needed.


Thank you for this.

As I recall, some posts to this list say that Windows provides
non-blocking i/o for sockets but not for files.

However, if non-blocking i/o for files were available, that
would be great. Actually, all I care about is input.

Can anyone provide a definite answer to this question?

And please, if the answer is YES (hope it is), with
working sample code.
The threaded approach does not help me. If the read blocks,
I do not know what to write. (I am responding to a command
line prompt - I have to read it first.)

--
Jonathan

Jul 18 '05 #3
Jonathan Fine a écrit :
Paul Rubin wrote: As I recall, some posts to this list say that Windows provides
non-blocking i/o for sockets but not for files.


No, Windows does provide non-blocking I/O for regular files, but it's a
completely different mechanism than the one used by winsock. You'll have
to use win32all and enter the Dark Side, that is Windows APIs.

You don't want to do that if you're not already familiar with
CreateProcess, CreatePipe, overlapped structures, WaitForSingleObject &
al...
Jul 18 '05 #4
fraca7 wrote:
Jonathan Fine a écrit :
Paul Rubin wrote:


As I recall, some posts to this list say that Windows provides
non-blocking i/o for sockets but not for files.

No, Windows does provide non-blocking I/O for regular files, but it's a
completely different mechanism than the one used by winsock. You'll have
to use win32all and enter the Dark Side, that is Windows APIs.

You don't want to do that if you're not already familiar with
CreateProcess, CreatePipe, overlapped structures, WaitForSingleObject &
al...

Thank you for this.

My application will, I think, become much more complicated if I cannot
use non-blocking input. (As already explained, it is not a problem
that can be solved by threads. Basically, I _need_ either to read
all available data, _or_ to read very carefully a byte at a time.)

Knowing that non-blocking input can be done under Windows, I would
like to use it. In the longer run, that will be easier than
rewriting my application. Or so it seems to me.

I did a google search, on the web, for
CreateProcess, CreatePipe, overlapped structures, WaitForSingleObject

This is the first page is produced
http://www.codeproject.com/threads/anonpipe1.asp

Seems to be the right sort of thing. But I don't have time to read it
now.

I'm not really a Windows programmer. Don't know the system calls.

But I do want my application to run on Windows.

I'll get back to this in a couple of weeks. (Busy right now.)
--
Jonathan

Jul 18 '05 #5

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

Similar topics

12
by: lothar | last post by:
re: 4.2.1 Regular Expression Syntax http://docs.python.org/lib/re-syntax.html *?, +?, ?? Adding "?" after the qualifier makes it perform the match in non-greedy or minimal fashion; as few...
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.
22
by: Steve - DND | last post by:
We're currently doing some tests to determine the performance of static vs non-static functions, and we're coming up with some odd(in our opinion) results. We used a very simple setup. One class...
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...
11
by: ypjofficial | last post by:
Hello All, So far I have been reading that in case of a polymorphic class ( having at least one virtual function in it), the virtual function call get resolved at run time and during that the...
2
by: Ian825 | last post by:
I need help writing a function for a program that is based upon the various operations of a matrix and I keep getting a "non-aggregate type" error. My guess is that I need to dereference my...
0
by: amitvps | last post by:
Secure Socket Layer is very important and useful for any web application but it brings some problems too with itself. Handling navigation between secure and non-secure pages is one of the cumbersome...
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...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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
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...

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.