Module to allow Asynchronous subprocess use on Windows and Posix platforms

Category: Python - Miscellaneous

The 'subprocess' module in Python 2.4 has made creating and accessing subprocess streams in Python relatively convenient for all supported platforms, but what if you want to interact with the started subprocess? That is, what if you want to send a command, read the response, and send a new command based on that response?

Now there is a solution. The included subprocess.Popen subclass adds three new commonly used methods: recv(maxsize=None), recv_err(maxsize=None), and send(input), along with a utility method: send_recv(input='', maxsize=None).

recv() and recv_err() both read at most maxsize bytes from the started subprocess.
send() sends strings to the started subprocess.
send_recv() will send the provided input, and read up to maxsize bytes from both stdout and stderr.

If any of the pipes are closed, the attributes for those pipes will be set to None, and the methods will return None.

v. 1.3 fixed a few bugs relating to *nix support
v. 1.4,5 fixed initialization on all platforms, a few bugs relating to Windows support, added two utility functions, and added an example of how to use this module.
v. 1.6 fixed linux _recv() and test initialization thanks to Yuri Takhteyev at Stanford.
v. 1.7 removed _setup() and __init__() and fixed subprocess unittests thanks to Antonio Valentino. Added 4th argument 'tr' to recv_some(), which is, approximately, the number of times it will attempt to recieve data. Added 5th argument 'stderr' to recv_some(), where when true, will recieve from stderr. Cleaned up some pipe closing.
v. 1.8 Fixed missing self. parameter in non-windows _recv method thanks to comment.
v. 1.9 Fixed fcntl calls for closed handles. Date: 16 May, 2012


Sysadmin

Homepage: http://code.activestate.com/recipes/440554-module-to-allow-asynchronous-subprocess-use-on-win/?in=lang-python

Developer: Josiah Carlson

License: Python License

Operating System: Windows

Add a Comment

all are required fields

     
What do you think of this resource?

Select Your Rate:

Votes:0

 

Related Scripts Download

Internet Billing Wizard 1.

developer Developer: http://tekmanproductions...
license License: Freeware
operating systems Operating System: Unix


Calculates IOPS for all disks in Linux/Unix system.

developer Developer: Slava Yanson
license License: Freeware
operating systems Operating System: Windows


RSH sometimes hangs if it cannot resolve to a given host.

developer Developer: Shrinidhi Rao
license License: Freeware
operating systems Operating System: Windows


System administrators sometimes need to automate commands which prompt for a password (or any other single prompt) before they execute.

developer Developer: John Landahl
license License: Python License
operating systems Operating System: Windows


Takes a stream of numbers and outputs simple ASCII graphs of those numbers.

developer Developer: Drew Gulino
license License: Freeware
operating systems Operating System: Windows


A Python script that runs arbitrary Python scripts in an input loop.

developer Developer: Drew Gulino
license License: Freeware
operating systems Operating System: Windows


LoggingWebMonitor listens for log records sent from other processes running in the same box or network.

developer Developer: Gabriel Genellina
license License: Freeware
operating systems Operating System: Windows


This recipe shows you how you might generate SQL code to insert the key-value pairs in a dictionary.

developer Developer: Carson Reynolds
license License: Python License
operating systems Operating System: Windows


this is a hack to get the "foo" module that's outside a package whithin a file of the package, when it also contains a module named "foo" (ie relative imports).

developer Developer: Tarek Ziade
license License: Python License
operating systems Operating System: Windows