Python time.sleep() vs event.wait()

前端 未结 3 1409
无人及你
无人及你 2020-11-30 22:49

I want to perform an action at a regular interval in my multi-threaded Python application. I have seen two different ways of doing it

exit = False
def thread         


        
3条回答
  •  孤城傲影
    2020-11-30 23:28

    It is interesting to note that the event.wait() method can be invoked on its own:

    from threading import Event # Needed for the  wait() method
    from time import sleep     
    
    print("\n Live long and prosper!")
    sleep(1)               # Conventional sleep() Method.
    print("\n Just let that soak in..")   
    Event().wait(3.0) # wait() Method, useable sans thread.
    print("\n Make it So! = )\n")
    

    So why -not- use wait() as an alternative to sleep() outside of multi-threading? In a word, Zen. (Of course.) Clarity of code is an important thing.

提交回复
热议问题