Managing a queue of subprocesses?

Tom Plunket tomas at fancy.org
Sat Dec 30 01:22:24 EST 2006


cypher543 wrote:

> self.buildPID = subprocess.Popen(buildCmd, stdout = subprocess.PIPE, stderr = subprocess.STDOUT)

Instead of calling it self.buildPID, you might just call it
self.buildProcess or something.  It's actually a Popen object that gets
returned.

So yes you can do what you want:

__init__ self.buildProcess to None.  Then, in execute(), if
(self.buildProcess is None) or (self.buildProcess.poll() is not None)
start the next process.

You've got to wait for one process to end before starting the next one,
it's really that easy.  So, don't just go ahead and fire them all
instantly.  Possibly what you want to do instead is have runQueue() do
the check somehow that there's no active process running.

What I would do, have runQueue() check to see if self.buildProcess is
None.  If it is None, fire the next process in the queue.  If it isn't
None, then check to see if it's ended.  If it has ended, then set
self.buildProcess to None.  Next UI update the next step in the queue
gets done.  Mind that you'll have to modify the queue as you go, e.g.
self.queue = self.queue[1:].

Finally, consider piping stderr separately, and direct its output to a
different window in your GUI.  You could even make that window pop open
on demand, if errors occur.

good luck,
-tom!

-- 



More information about the Python-list mailing list