Timer scene
from threading import Timer
class CustomTimer(Timer):
def run(self): while not self.finished.is_set(): self.function(*self.args, **self.kwargs) self.finished.wait(self.interval) class Command(BaseCommand): def handle(self, *args, **options): try: if get_execute_flag(): init_conn() timer_list = [ # Market homepage data cache CustomTimer(1.0, timed_refresh_market_home_page_data), # Invitation ranking event switch cache CustomTimer(1.0, timed_refresh_invite_leaderboard_activity_switch_data), ] # Start caching for t in timer_list: t.start() while True: if not get_execute_flag(): # Cancel scheduled caching for t in timer_list: t.cancel() break time.sleep(5) except: write_error_log(\'main\') finally: time.sleep(5)
timer source code
First analyze the wait source code
When wait is used, lock’s wait