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.Pope n 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 CancellationExc eption(Exceptio n): pass
class CancellableComm and:
def __init__(self, command, stdout=nullFile , stderr=nullFile ,
refreshDelay=0. 1, raiseIfNonZero= True):
self.command = command
self.stdout = stdout
self.stderr = stderr
self.refreshDel ay = refreshDelay
def execute(self, cancelEvent=Non e):
if cancelEvent is None:
cancelEvent = threading.Event () # dummy
exitCode = None
cmd = subprocess.Pope n(
self.command, stdout=self.std out, stderr=self.std err)
while exitCode is None:
# Wait for event to be set for a specific delay
cancelEvent.wai t(self.refreshD elay)
if cancelEvent.isS et():
kill(cmd.pid) # implemented as in FAQ
raise CancellationExc eption(
'Process "' + self.command + '" cancelled')
exitCode = cmd.poll()
return exitCode
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.Pope n 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 CancellationExc eption(Exceptio n): pass
class CancellableComm and:
def __init__(self, command, stdout=nullFile , stderr=nullFile ,
refreshDelay=0. 1, raiseIfNonZero= True):
self.command = command
self.stdout = stdout
self.stderr = stderr
self.refreshDel ay = refreshDelay
def execute(self, cancelEvent=Non e):
if cancelEvent is None:
cancelEvent = threading.Event () # dummy
exitCode = None
cmd = subprocess.Pope n(
self.command, stdout=self.std out, stderr=self.std err)
while exitCode is None:
# Wait for event to be set for a specific delay
cancelEvent.wai t(self.refreshD elay)
if cancelEvent.isS et():
kill(cmd.pid) # implemented as in FAQ
raise CancellationExc eption(
'Process "' + self.command + '" cancelled')
exitCode = cmd.poll()
return exitCode
Comment