Managing a queue of subprocesses?

Tom Plunket tomas at fancy.org
Sun Dec 31 19:50:47 EST 2006


cypher543 wrote:

> That was a very good answer, and it sure sounds like it would work.
> However, I failed at implementing it. :( My updated runQueue() function
> is:
> 
> def runQueue(self):
> 	self.buildProcess = None
> 	count = 1 # current position in the queue
> 	while True:
> 		if self.buildProcess is None:
> 			self.execute(self.cmdQueue[count][2], self.cmdQueue[count][0],
> self.cmdQueue[count][1])
> 			count = count + 1
> 		else:
> 			# I'm not really sure what to put here
> 
> I pretty sure I did all of that wrong. ;) Also, how exactly would I
> redirect stderr to another place?

You're thinking too hard.  ;)

class Whatever:
   def __init__(self):
      self.process = None
      # and other stuff, probably.

   def runQueue(self):
      # check to see if no process has been started, or if
      # the most-recently-started one has finished.
      if not (self.process or (self.process.poll() is None)):
         if self.process:
            previousReturnCode = self.process.returncode

         if len(self.cmdQueue) > 0:
            command = self.cmdQueue.pop(0) # pull off the first command.
            self.execute(command[2], command[0], command[1])
         else:
            self.process = None

...then, to prevent your GUI from freezing, you need to just call this
function in your idle handling.  If you don't want to start all of your
commands at once, you need to not start all your commands at once.  ;)


-tom!

-- 



More information about the Python-list mailing list