wxpython - Running threads sequentially without blocking GUI
        Posted  
        
            by 
                ryantmer
            
        on Stack Overflow
        
        See other posts from Stack Overflow
        
            or by ryantmer
        
        
        
        Published on 2012-10-12T21:08:42Z
        Indexed on 
            2012/10/15
            15:38 UTC
        
        
        Read the original article
        Hit count: 258
        
I've got a GUI script with all my wxPython code in it, and a separate testSequences module that has a bunch of tasks that I run based on input from the GUI. The tasks take a long time to complete (from 20 seconds to 3 minutes), so I want to thread them, otherwise the GUI locks up while they're running. I also need them to run one after another, since they all use the same hardware. (My rationale behind threading is simply to prevent the GUI from locking up.) I'd like to have a "Running" message (with varying number of periods after it, i.e. "Running", "Running.", "Running..", etc.) so the user knows that progress is occurring, even though it isn't visible. I'd like this script to run the test sequences in separate threads, but sequentially, so that the second thread won't be created and run until the first is complete. Since this is kind of the opposite of the purpose of threads, I can't really find any information on how to do this... Any help would be greatly appreciated.
Thanks in advance!
gui.py
import testSequences
from threading import Thread
#wxPython code for setting everything up here...
for j in range(5):
    testThread = Thread(target=testSequences.test1)
    testThread.start()
    while testThread.isAlive():
        #wait until the previous thread is complete
        time.sleep(0.5)
        i = (i+1) % 4
        self.status.SetStatusText("Running"+'.'*i)
testSequences.py
import time
def test1():
    for i in range(10):
        print i
        time.sleep(1)
(Obviously this isn't the actual test code, but the idea is the same.)
© Stack Overflow or respective owner