Managing a queue of subprocesses?

cypher543 david at cypherspace.info
Fri Dec 29 20:26:44 EST 2006


My app uses a "queue" of commands which are run one at a time. I am
using the subprocess module to execute the commands in the queue.
However, processes always run at the same time. How can I make one
process run at a time, and then execute the next process when the first
has terminated? My code is below:

self.cmdQueue = {}
self.queue(theProject.directory, "ls", "-l")
self.queue(theProject.directory, "echo", "hello, world!")

def consoleLogAddLine(self, text):
	self.consoleLogBuffer.insert(self.consoleLogBuffer.get_end_iter(),
text)
	self.consoleLog.scroll_to_mark(self.consoleLogBuffer.get_insert(), 0)

def onGetData(self, fd, cond, *args):
	self.consoleLogAddLine(fd.readline())
	return True

def queue(self, rootDir, cmd, args = ""):
	count = len(self.cmdQueue) + 1
	self.cmdQueue[count] = [cmd, args, rootDir]

def runQueue(self):
	for i in self.cmdQueue.values():
		self.execute(i[2], i[0], i[1])

def execute(self, rootDir, cmd, args = ""):
	os.chdir(rootDir)
	if args == "":
		buildCmd = cmd
	else:
		args = args.split(" ")
		buildCmd = [cmd] + args
	self.buildPID = subprocess.Popen(buildCmd, stdout = subprocess.PIPE,
stderr = subprocess.STDOUT)
	gobject.io_add_watch(self.buildPID.stdout, gobject.IO_IN,
self.onGetData)

As you can see, I add the commands "ls -l" and "echo Hello" to the
queue. However, "Hello" is always printed inside the output of "ls -l".
I would like to wait for "ls -l" to terminate and then run "echo
Hello". But, the output must still print to the consoleLogBuffer
line-by-line, and my GUI must not hang during execution.

Is this even possible?




More information about the Python-list mailing list