Cancellable threading.Timer in Python

后端 未结 4 1477
南旧
南旧 2020-12-02 13:15

I am trying to write a method that counts down to a given time and unless a restart command is given, it will execute the task. But I don\'t think Python threading.Tim

4条回答
  •  一个人的身影
    2020-12-02 14:01

    I'm not sure if I understand correctly. Do you want to write something like in this example?

    >>> import threading
    >>> t = None
    >>> 
    >>> def sayHello():
    ...     global t
    ...     print "Hello!"
    ...     t = threading.Timer(0.5, sayHello)
    ...     t.start()
    ... 
    >>> sayHello()
    Hello!
    Hello!
    Hello!
    Hello!
    Hello!
    >>> t.cancel()
    >>>
    

提交回复
热议问题