-1

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(test) 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.

3
  • Why would you do it this way? This is exactly what people are talking about when they say someone is "abusing eval". This entire method is wrong and redirecting output is not going to fix it. Commented Aug 18, 2011 at 12:14
  • What method should I use then? Please enlighten me as I'm quite new to python programming Commented Aug 18, 2011 at 12:16
  • 1
    I can't answer this as you haven't made it clear what exactly you're doing, I made a suggestion in my answer to your previous question. Did you try just testmodule = __import__(test), then ret = getattr(testmodule, test)()? That would at least be better than eval. Also, why is your test printing the output instead of using yield or return? This is wrong in any language to output info from a function. Commented Aug 18, 2011 at 12:22

1 Answer 1

1

The solution to your immediate problem is to use two Queue.Queues 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.

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.