Want to read Slashdot from your mobile device? Point it at m.slashdot.org and keep reading!


Forgot your password?
DEAL: For $25 - Add A Second Phone Number To Your Smartphone for life! Use promo code SLASHDOT25. Also, Slashdot's Facebook page has a chat bot now. Message it for stories and more. Check out the new SourceForge HTML5 Internet speed test! ×

Journal morgan_greywolf's Journal: Asynchronous (non-blocking) I/O for subprocesses in Python 1

In case you're just immediately drooling to find the answer to this conundrum, I found it on ASPN: a module to allow asynchronous subprocess I/O through a pipe that works on both POSIX and Windows.

Anyway the full story: I started looking up the necessary pywin32 calls (along with their corresponding C Win32 API calls, since the pywin32 documentation sucks) to do it -- I knew I might need to create a pipe with CreatePipe and set it as FILE_FLAG_OVERLAPPED and then pass it CreateProcess via the startupinfo parameter. I then figured out that I could do most of this by subclassing subprocess.Popen. I also found PeekNamedPipe, which also might do what I needed ... and then, voila, after searching for some Python/Pywin32 examples that used a few of these API calls, I came across the above-linked recipe on ASPN.

(I already knew I could achieve what I wanted on Unix by subclassing Popen and using select and fcntl and knew how to do it. It was just convenient that the above happened to already be using the technique for POSIX OSes)

It would be really cool if that noah guy could add support for this technique in Pexpect, and in fact, it's the next logical step. I may, for my project, end up subclassing Pexpect to write my own implementation using asynchronous pipe I/O rather than a pty. This has several disadvantages, I know, but for most of what I use Expect or Pexpect for, it won't make one bit of difference.

One thing, though, is that I don't know if there is any problem with using code posted on ASPN in a GPL v2 or v3 application. Anyone know the details? And, there seem to be some disdvantages listed on the linked article, such as Cygwin's bash and sh not displaying prompts and problems with Python code on Windows. Anyone know how to fix those?

This discussion has been archived. No new comments can be posted.

Asynchronous (non-blocking) I/O for subprocesses in Python

Comments Filter:

Too many people are thinking of security instead of opportunity. They seem more afraid of life than death. -- James F. Byrnes