Python - wait on a condition without high cpu usage

后端 未结 3 1407
我在风中等你
我在风中等你 2021-01-16 05:22

In this case, say I wanted to wait on a condition to happen, that may happen at any random time.

 while True:
    if condition:
        #Do Whatever
    else         


        
3条回答
  •  梦毁少年i
    2021-01-16 06:12

    As already mentioned you can a) poll i.e. check for a condition and if it is not true wait for some time interval, if your condition is an external event you can arrange for a blocking wait for the state to change, or you can also take a look at the publish subscribe model, pubsub, where your code registers an interest in a given item and then other parts of the code publish the item.

提交回复
热议问题