7
votes

Possible Duplicate:
Can select() be used with files in Python under Windows?

On UNIX I am able to pass sys.stdin to select.select in Python. I am attempting to do this on Windows, but select.select in Python on Windows will not allow it.

To more accurately describe what I am doing see https://github.com/eldarion/gondor-client/blob/ccbbf9d4b61ecbc2f66f510b993eb5fba0d81c09/gondor/run.py.

The unix_run_poll function is what I am trying to accomplish on Windows. The basic idea is that I have a socket connection to a server which has hooked up streaming stdin, stdout, stderr to a process running remotely and I am interacting with it from the local client and making it appear as if the local client is running the process.

The win32_run_poll is my attempt at porting it to Windows and it does work, sort of. It is a little wonky and the approach, IMO, is very bad.

Does anyone have suggestions on how this can be improved? The dependency on win32api is less than ideal, but I am okay with keeping it.

1
Have you looked at libraries/frameworks dedicated to event loop implementations? For example, twisted has two implementations of a Win32 reactor. - user4815162342
Funny you mention that. I happened to be doing that right now. :-) - Brian Rosner
Another possibility is to actually give your script the socket, instead of wrapping it up as the script's stdin/stdout, and then you can just use select.select. - abarnert
I didn't phrase my question well enough. I, as the poster, know that you can't use sys.stdin in select on Windows. The question you linked was giving that answer. I don't want that answer. If you look at the code I linked it would indicate that I clearly understand the problem better than my question title may infer. - Brian Rosner

1 Answers

7
votes

On Windows select is only defined for sockets, and will not work for arbitrary file handles (windows has no concept of file descriptors). For more information about this issue, see the msdn documentation, it is also mentioned in the python documentation for the select module.

If you want to use polling for arbitary files, you should look into something that abstracts polling sockets and file handles. This might be the twisted reactor referred to in a comment to your post, or it might be a python binding to libuv, or some other event library of your choice.