How to use subprocess

N

Nicolas Fleury

Hi,
I want to use the subprocess module (or any standard Python module) to
run a process:
- which stdout and stderr can each be redirected to any file-like object
(no fileno function).
- can be cancelled with a threading.Event.

My problem is that the subprocess.Popen constructor doesn't seem to
support file-like objects (only file objects with fileno()).

If I use subprocess.PIPE, I have the problem that the read functions of
of the subprocess objects stdout and stderr are blocking (and I want to
redirect them to different file-like objects, so I definitely need
non-blocking access). How am I supposed to do it?

Thx and regards,
Nicolas

It something like that that I would need to make work (but supporting
file-like objects):

class CancellationException(Exception): pass
class CancellableCommand:
def __init__(self, command, stdout=nullFile, stderr=nullFile,
refreshDelay=0.1, raiseIfNonZero=True):
self.command = command
self.stdout = stdout
self.stderr = stderr
self.refreshDelay = refreshDelay
def execute(self, cancelEvent=None):
if cancelEvent is None:
cancelEvent = threading.Event() # dummy
exitCode = None
cmd = subprocess.Popen(
self.command, stdout=self.stdout, stderr=self.stderr)
while exitCode is None:
# Wait for event to be set for a specific delay
cancelEvent.wait(self.refreshDelay)
if cancelEvent.isSet():
kill(cmd.pid) # implemented as in FAQ
raise CancellationException(
'Process "' + self.command + '" cancelled')
exitCode = cmd.poll()
return exitCode
 
D

Dennis Lee Bieber

Hi,
I want to use the subprocess module (or any standard Python module) to
run a process:

Pardon the question, but which version of Python /has/ a
"subprocess" module? Is it new with 2.4?

I've not installed 2.4 yet as I'm not sure I can locate
pre-built external modules for some of the stuff I currently use.

--
 
M

Michele Simionato

I am not sure how safe it is, but on Linux I have just copied
subprocess.py
in my 2.3 installation and it worked in all the cases I tried.

Michele Simionato
 
T

Thomas Bellman

Nicolas Fleury said:
Hi,
I want to use the subprocess module (or any standard Python module) to
run a process:
- which stdout and stderr can each be redirected to any file-like object
(no fileno function).
- can be cancelled with a threading.Event.
My problem is that the subprocess.Popen constructor doesn't seem to
support file-like objects (only file objects with fileno()).
If I use subprocess.PIPE, I have the problem that the read functions of
of the subprocess objects stdout and stderr are blocking (and I want to
redirect them to different file-like objects, so I definitely need
non-blocking access). How am I supposed to do it?

You might take a look at my asyncproc module, available at

http://www.lysator.liu.se/~bellman/download/asyncproc.py

Specifically the Process class. It doesn't do exactly what you
want, but maybe you can use it as inspiration for doing it yourself.

It requires the subprocess module, but I have successfully used
it under Python 2.3.2 with subprocess installed locally.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,995
Messages
2,570,230
Members
46,818
Latest member
Brigette36

Latest Threads

Top