检测系统在循环中暂停(Detecting system suspend in a loop)

编程入门 行业动态 更新时间:2024-10-07 09:25:56
检测系统在循环中暂停(Detecting system suspend in a loop)

我试图使用以下算法检测系统暂停:

while True: lastchecked = now() if now() - lastchecked > 1s: print "suspend detected!"

但是我遇到了一个问题:如果在第二行和第三行之间发生暂停,那么循环会捕获它。 但是如果暂停发生在第一行和第二行之间,那么算法将失败。

这种情况有没有一些常用的方法? 最好是独立于操作系统的请,我不想挂钩到OS事件等。

I'm trying to detect system suspend using the following algorithm:

while True: lastchecked = now() if now() - lastchecked > 1s: print "suspend detected!"

But I ran into a problem: If suspend happens between 2nd and 3rd line, then the loop catches it. But if suspend happens between 1st and 2nd line, then the algorithm fails.

Is there some commonly used method for this type of situation? Preferably OS-independent please, I don't want to hook into OS events and suchlike.

最满意答案

首先, 轮询是推断通知的原因,因为它浪费了系统资源,而这些资源可能会花费在有用的工作上(并且当前的循环也是繁忙的循环 )。 当然,电源管理事件系统是特定于操作系统的(请参阅Linux中的电源管理通知以及如何使用python在窗口中挂接事件/消息 ),但是如果您正在编写系统监视器应用程序,则无论如何都无法隐藏操作系统差异。


现在,关键在于在内存中始终有两个时间戳,并覆盖较旧的时间戳:

T1 \ T2 <- compare / T3 <- compare \ T4 etc /

然后,在暂停发生的任何时刻,下一个时间戳会比它应该设置的时间晚,并且比较会看到差异。

这样,你甚至不需要每秒钟轮询! 您的投票时间间隔只需与您希望检测的最短暂停时间一样短。 例如,如果您想要检测至少30秒的暂停时间,则只需每隔30秒进行一次轮询:如果系统睡眠时间更长,则会保证“错过一个节拍”。

i=0 poll_period=30 t=[time.time()]*2 while True: # actually, poll period will be slightly longer due to code overhead: # https://stackoverflow.com/questions/26774186/looping-at-a-constant-rate-with-high-precision-for-signal-sampling # but that doesn't make a difference in this case time.sleep(poll_period) t[i]=time.time() if t[i] - t[(i+1)%2] > poll_period + 2: print "suspend detected" i = (i+1)%2

请注意, 如果您的流程被其他人抢占 , 您将获得误报 。 这是为什么使用系统通知是一种非常优越的方式的另一个原因。

First of all, polling is inferiour to notifications because it wastes system resources that could instead be spent on useful work (and your current loop is a busy loop, too). Naturally, power management event systems are OS-specific (see Power Management Notifications in Linux and how to hook to events / messages in windows using python) but if you're writing a system monitor app, you can't hide from OS differences anyway.


Now, the key here is to always have two timestamps in memory and overwrite the older one:

T1 \ T2 <- compare / T3 <- compare \ T4 etc /

Then, at whichever moment a suspend happens, the next timestamp will be set later than it should, and the comparison will see the difference.

This way, you don't even need to poll every second or so! Your poll interval needs to be only as short as is the shortest suspend period that you'd like to detect. E.g. if you'd like to detect at least a 30s suspend period, you only have to poll every 30s: if the system sleeps for longer, it will be guaranteed to "miss a beat".

i=0 poll_period=30 t=[time.time()]*2 while True: # actually, poll period will be slightly longer due to code overhead: # https://stackoverflow.com/questions/26774186/looping-at-a-constant-rate-with-high-precision-for-signal-sampling # but that doesn't make a difference in this case time.sleep(poll_period) t[i]=time.time() if t[i] - t[(i+1)%2] > poll_period + 2: print "suspend detected" i = (i+1)%2

Note that you will get false positives if your process gets preempted by others. That's yet another reason why using system notifications is a vastly superior way.

更多推荐

本文发布于:2023-08-07 17:00:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1465264.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:检测系统   Detecting   system   suspend   loop

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!