I asked a question yesterday about a problem with a program I'm writing in Python ( Passing wxPython objects as multiprocessor arguments ). I managed to solve that problem by using a multiprocess process for the function that evaluates the scripts. However, since the scripts are runned from a different process, their output is not properly redirected to my wxPython TextCtrl window. So, I'm looking for way to continously redirect the output from the childprocess to my main process so it can be written to my text window.
This is the function that sets up the process:
def createprocess(test):
q = Queue()
q.put(tes开发者_StackOverflow中文版t)
p = Process(target=runtest, args=(q,))
p.start()
p.join()
return q.get()
This is the target function of the process:
def runtest(q):
test = q.get()
exec 'import ' + test
func=test+'.'+test+'()'
ret = eval(func)
q.put(ret)
I found this thread ( How can I send python multiprocessing Process output to a Tkinter gui ) which describes how to redirect output from the childprocess but the problem was that the output was received after the evaluation was complete.
The solution to your immediate problem is to use two Queue.Queue
s instead of just one. Let's call them inqueue
and outqueue
. Then
def runtest(inqueue,outqueue):
test = inqueue.get()
module=__import__(test)
ret=getattr(module,test)()
outqueue.put(ret)
The larger issue concerns how to control GUI elements like the TextCtrl
with the output from a separate process. The answer is you don't. Instead spawn a thread (which can if you like spawn other processes), which can receive the values from outqueue
and (unlike separate processes) update the TextCtrl
.
See the LongRunningTasks wiki for examples on how to set this up.
精彩评论