"Timer already started or running, please wait if you're restarting.") In the interest of providing a correct answer using Timer as the OP requested, I'll improve upon from threading import Timerĭef _init_(self, seconds, target, args=, kwargs=dict()): Print("Hello in " + str(self.sleepTime) + " seconds") Self.threadNew = threading.Thread(target=self.foo_target) Raise Exception("This class is a singleton!") Please tell me if any memory leakage is here. I have come up with another solution with SingleTon class. Try to change "dalay" variable and you will able to see difference from threading import Timer, _sleep Here is small sample, it will help beter understanding how it runs.įunction taskManager() at the end create delayed function call to it self. Timer_2 = RepeatingAsyncTimer(interval=10, cb=cb, timer_name='timer_2') Timer_1 = RepeatingAsyncTimer(interval=5, cb=cb, timer_name='timer_1') Self.aio_timer = Timer(delay=self.interval, In addition to the above great answers using Threads, in case you have to use your main thread or prefer an async approach - I wrapped a short class around aio_timers Timer class (to enable repeating) import asyncioĭef _init_(self, interval, cb, *args, **kwargs): Questions = ["What is the capital of Italy?",Īnswers = "Rome", "Paris", "London", "Madrid" Lab = tk.Label(app, text="Timer will start in a sec")Ĭlock = "".format(tempo.hour, tempo.minute, cond), end="") This code puts the clock timer in a little window with tkinter from threading import Timer, Thread, Event Self.thread = Timer(self.t, self.handle_function) from threading import Timer, Thread, Event I have changed some code in swapnil-jariwala code to make a little console clock. Print("Timer never started or failed to initialize.") Self._should_continue = False # Just in case thread is running and cancel fails. Print("Timer already started or running, please wait if you're restarting.") If not self._should_continue and not self.is_running: Self.thread = Timer(conds, self._handle_target) If self._should_continue: # Code could have been running when cancel was called. """A Timer class that does not stop, unless you want it to.""" In the interest of providing a correct answer using Timer as the OP requested, I'll improve upon swapnil jariwala's answer: from threading import Timer
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |