]>
code.delx.au - bg-scripts/blob - lib/commands_async.py
f90a2a52d92daafccb7168c2656a000d8582b2a3
4 A small utility that provides similar functionality to the commands module, but
5 allows you to get the output from multiple processes at the same time
8 __author__
= "Greg Darke"
10 import subprocess
, fcntl
, os
11 from select
import select
13 import cStringIO
as _StringIO
15 import StringIO
as _StringIO
17 class CommandRunner(object):
23 def _executeCommand(self
, cmd
):
24 """Execute the command"""
25 output
= _StringIO
.StringIO()
26 isShell
= isinstance(cmd
, str)
27 process
= subprocess
.Popen(args
= cmd
, shell
= isShell
,
28 stdout
= subprocess
.PIPE
, stderr
= subprocess
.STDOUT
)
31 flags
= fcntl
.fcntl(process
.stdout
, fcntl
.F_GETFL
) | os
.O_NONBLOCK
32 fcntl
.fcntl(process
.stdout
, fcntl
.F_SETFL
, flags
)
34 return (output
, process
)
36 def executeCommand(self
, cmd
):
37 """Executes a command, but does not return anything"""
38 output
, process
= self
._executeCommand
(cmd
)
39 self
._outputs
[process
.stdout
] = output
40 self
._processes
[cmd
] = process
41 self
._fds
.append(process
.stdout
)
46 # While there are still some processes left
47 inputReady
, outputReady
, exceptReady
= \
48 select(self
._fds
, [], self
._fds
)
55 self
._outputs
[fd
].write(data
)
57 for fd
in exceptReady
:
60 def waitForCompletion(self
):
61 """Waits for all of the running processes to finish"""
65 """Returns a dictionay containing the command as the key, and a string (of the output) as the value"""
66 outputs
= dict((cmd
, self
._outputs
[process
.stdout
].getvalue()) for cmd
, process
in self
._processes
.items())