No I am not sure how to do a subthread. I thought threading was more for
Windows platform?
Threading is just a tool. Sometimes threading is useful. Sometimes its not. On some platforms its more or less. In a GUI, threading is often useful. The alternative is to use non-blocking actions and check stuff in idle handlers-- which is possible. I doubt one is really easier then the other.
import threading
def handle_output(control, cmd):
child = subprocess.Popen(cmd, shell=True, stdout=subprocess.PIPE)
child.wait()
output = child.stdout.read()
wx.CallAfter(control.Append
, output)
Then when you run this command, do:
thread = threading.Thread(target=handle_output, args=(self.listCtrl, cmd))
thread.run()
etc. Of course you might have to adjust some of that. (I rarely use the subprocess module, so may hav the output fetching wrong). Just be sure that when “calling back” into the main/GUI thread from your subthread, you use wx.CallAfter, so instead of control.Append(output) you wx.CallAfter(control, output)
Depending on how you want to do it, if you want to have these commands run sequentially, you might do a thread differently and have a Queue (see python docs) that it blocks on, and then just pass (control, command) into that Queue when you’re ready. So you don’t construct a new thread each time, but just have a single worker thread. It just all depends on what you’re doing.
But… there’s nothing wrong with threads Threads are great and useful tools. Especially in GUI programming where you have to keep the ‘main thread’ unoccupied if you want responsiveness.
–5