Pausing a thread using threading class

后端 未结 6 1442
情歌与酒
情歌与酒 2020-12-08 07:41

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

thread         


        
6条回答
  •  温柔的废话
    2020-12-08 08:20

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

提交回复
热议问题