How to pause and resume a thread using the threading module?

Viewed 47311

I have a long process that I've scheduled to run in a thread, because otherwise it will freeze the UI in my wxpython application.

I'm using:

threading.Thread(target=myLongProcess).start()

to start the thread and it works, but I don't know how to pause and resume the thread. I looked in the Python docs for the above methods, but wasn't able to find them.

Could anyone suggest how I could do this?

6 Answers

I had the same issue. It is more effective to use time.sleep(1800) in the thread loop to pause the thread execution.

e.g

MON, TUE, WED, THU, FRI, SAT, SUN = range(7) #Enumerate days of the week
Thread 1 : 
def run(self):
        while not self.exit:
            try:
                localtime = time.localtime(time.time())
                #Evaluate stock
                if localtime.tm_hour > 16 or localtime.tm_wday > FRI:
                    # do something
                    pass
                else:
                    print('Waiting to evaluate stocks...')
                    time.sleep(1800)
            except:
                print(traceback.format_exc())

Thread 2
def run(self):
    while not self.exit:
        try:
            localtime = time.localtime(time.time())
            if localtime.tm_hour >= 9 and localtime.tm_hour <= 16:
                # do something
                pass
            else:
                print('Waiting to update stocks indicators...')
                time.sleep(1800)
        except:
            print(traceback.format_exc())
Related