Equivalent of setInterval in python

前端 未结 4 1762
予麋鹿
予麋鹿 2020-12-13 10:21

I have recently posted a question about how to postpone execution of a function in Python (kind of equivalent to Javascript setTimeout) and it turns out to be a

4条回答
  •  庸人自扰
    2020-12-13 10:57

    Your solution looks fine to me.

    There are several ways to communicate with threads. To order a thread to stop, you can use threading.Event(), which has a wait() method that you can use instead of time.sleep().

    stop_event = threading.Event()
    ...
    stop_event.wait(1.)
    if stop_event.isSet():
        return
    ...
    

    For your thread to exit when the program is terminated, set its daemon attribute to True before calling start(). This applies to Timer() objects as well because they subclass threading.Thread. See http://docs.python.org/library/threading.html#threading.Thread.daemon

提交回复
热议问题