Simple process IO capture (Was: "shell-commands" and python!) - process.py (0/1)

Paul Moore gustav at morpheus.demon.co.uk
Sun Sep 23 16:59:48 EDT 2001


On Sat, 22 Sep 2001 15:27:24 +0100, Paul Moore <gustav at morpheus.demon.co.uk>
wrote:

>While I don't have a problem with the philosophy of using the right tool for the
>job, I think there is a fundamental "type of task" here - namely, pipe-type
>plumbing of subprocesses - which is fairly common. It would not do any harm to
>have a relatively simple way of doing this in Python.
>
>However, this does *not* mean that there's any need to change anything - it's an
>ideal task for a new module (like popen/fork and friends, but higher level). To
>answer the original poster's question in a different way, I am not awaer of such
>a module which currently exists, but I imagine that designing and writing such a
>module would be an interesting, and generally useful task. Heck, i may even have
>a look at it myself...

I did some looking. The attached is a fairly simple prototype of a module for
process handling. The simple examples

    >>> output = process.run("echo hello")
    >>> print output
    hello
    >>> print process.run("tr a-z A-Z", input="hello")
    HELLO

are similar to shell "backticks", with the ability to provide input. There are
features to allow merging of stdout and stderr, raise an exception if the
command fails (the default action is to continue, but make the return code
available as process.retval()), and set teh mode of the pipe (text or binary).

It's a fairly simple wrapper around popen2/popen4, but it seems fairly useful to
me. Comments anyone?

I have ideas to work on this - things like allowing a sequence of strings as the
"command", which acts like a Unix pipe (the plumbing is messy to set up, so
encapsulating this might be nice...) or maybe some sort of class-based interface
(but the simplicity of the examples above is important, and should remain).

What do people think? Is this a useful idea?

Paul Moore.



More information about the Python-list mailing list