7

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.

Community
  • 1
  • 1
Brian Rosner
  • 694
  • 4
  • 9
  • 1
    Have you looked at libraries/frameworks dedicated to event loop implementations? For example, [twisted](http://twistedmatrix.com/trac/) has two implementations of a [Win32 reactor](http://twistedmatrix.com/documents/current/core/howto/choosing-reactor.html#win32_wfmo). – user4815162342 Sep 19 '12 at 17:54
  • Funny you mention that. I happened to be doing that right now. :-) – Brian Rosner Sep 19 '12 at 17:55
  • 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 Sep 19 '12 at 18:15
  • 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 Sep 20 '12 at 02:20
  • Related: [Can select() be used with files in Python under Windows?](http://stackoverflow.com/questions/10842428/) – Piotr Dobrogost Sep 20 '12 at 06:26

1 Answers1

7

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.

dnaq
  • 2,104
  • 1
  • 14
  • 24
  • 1
    Another option would be to call [`WaitForMultipleObjects`](http://msdn.microsoft.com/en-us/library/windows/desktop/ms687025%28v=vs.85%29.aspx) via [pywin32](http://starship.python.net/crew/mhammond/win32/Downloads.html). – Adam Rosenfield Sep 19 '12 at 18:28
  • 1
    This isn't quite accurate. The Python documentation says "On Windows, the underlying select() function is provided by the WinSock library, and does not handle file descriptors that don’t originate from WinSock." That's also not quite accurate. As you say, Windows has no concept of file descriptors—but the C library (MSVCRT) does, and so does the WinSock library. The problem is that they're not the same concept. (To make things even more fun, modern versions of WinSock don't actually use file descriptors in select; an fd_set is actually an array of handles…) – abarnert Sep 19 '12 at 18:32
  • Seems like WaitForMultipleObjects/WaitForSingleObject is undefined for file handles, but they can be used for console input. See http://msdn.microsoft.com/en-us/library/windows/desktop/ms687025(v=vs.85).aspx. – dnaq Sep 19 '12 at 18:33
  • @danvari: Passing a file handle to a `Wait` function will wait until the next I/O on that file completes. If you have a non-overlapped file handle, there is only one outstanding I/O at a time, and waiting can be useful. If you have an overlapped handle, there can be multiple concurrent I/Os, and you can't tell which one completed from just the file handle. – Gabe Nov 09 '13 at 07:09