开发者

Simple alarm clock and checking time every second

I'm creating an alarm clock in python and have a simple question:

My alarm clock's resolution needs to be down to the second. Currently I have a thread that wakes every 0.5 seconds, and checks whether its time to start making noise.

My only concern is that somehow the system gets completely overloaded at some point and the alarm does not sound because the alarm clock thread was still sleeping during the second it was supposed to开发者_C百科 check the time.

So my question is, would checking every 0.05 seconds be better? Or is there a way to register an event handler with python for a specific time, so I dont have to keep checking the time myself.


Check the standard signal module for timer functions.

Also, if you already know when to trigger your action, why not execute time.sleep(seconds) just before executing it? (seconds should be the time left to trigger the action in seconds, obviously)


That's not the best solution. It's no guarantee you are locked out for more than one second.

You should stick to 0.5 second resolution (or worse if no lives depend on it) and fire the event whenever it is at or past the due time, and keep a flag per event if it has been fired. Then it will always be fired at exactly once, and as close to the target time as achievable.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜