Python中的单调监管计时器
我有一个Python代码,它催生了另一个过程。给孩子20秒以执行。如果那时还没有完成,则需要被杀死。
主:
#!/usr/bin/python
from subprocess import Popen, PIPE
from threading import Timer
proc = Popen(['./plugin.sh'], stdout=PIPE, stderr=PIPE, universal_newlines=True)
killer = Timer(20, proc.kill)
killer.start()
for line in proc.stdout:
print('stdout: ' + line)
stdout, stderr = proc.communicate()
用于测试的样本无限儿童:
# cat plugin.sh
#!/bin/sh
count=1
while true; do
echo "$(date) hello [$((count++))]"
sleep 1
done
正常执行良好,20秒后,孩子被杀死。
stdout: Wed 01 Jun 2022 04:07:52 PM IST hello [1]
stdout: Wed 01 Jun 2022 04:07:53 PM IST hello [2]
...
stdout: Wed 01 Jun 2022 04:08:10 PM IST hello [19]
stdout: Wed 01 Jun 2022 04:08:11 PM IST hello [20]
#
现在,我希望主要程序在20秒后杀死儿童,而与系统时间更改无关。 使用当前代码,如果我手动更改系统时间,孩子的时间比20秒的津贴要少或更多。
例如,当我向后更改时间时:
stdout: Wed 01 Jun 2022 04:14:44 PM IST hello [4]
stdout: Wed 01 Jun 2022 02:59:25 PM IST hello [5]
...
stdout: Wed 01 Jun 2022 02:59:41 PM IST hello [21]
stdout: Wed 01 Jun 2022 02:59:42 PM IST hello [22]
...
在此处找到相关的讨论: https://bugs.pys.python.org/issue31267 < /a>
# python
Python 3.7.13 (default, May 27 2022, 08:39:11)
[GCC 8.4.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import sys
>>> sys.thread_info
sys.thread_info(name='pthread', lock='semaphore', version='NPTL 2.30')
>>>
我在Linux上,使用的锁定是信号量,而不是Mutex+Cond。
我可以用来使计时器独立于系统时间更改的替代机制?
I have a python code, which spawns another process. The child is given 20 seconds for execution. If not finished by that time, it needs to be killed.
Main:
#!/usr/bin/python
from subprocess import Popen, PIPE
from threading import Timer
proc = Popen(['./plugin.sh'], stdout=PIPE, stderr=PIPE, universal_newlines=True)
killer = Timer(20, proc.kill)
killer.start()
for line in proc.stdout:
print('stdout: ' + line)
stdout, stderr = proc.communicate()
Sample infinite child used for testing:
# cat plugin.sh
#!/bin/sh
count=1
while true; do
echo "$(date) hello [$((count++))]"
sleep 1
done
Normal execution is fine, child gets killed after 20 seconds.
stdout: Wed 01 Jun 2022 04:07:52 PM IST hello [1]
stdout: Wed 01 Jun 2022 04:07:53 PM IST hello [2]
...
stdout: Wed 01 Jun 2022 04:08:10 PM IST hello [19]
stdout: Wed 01 Jun 2022 04:08:11 PM IST hello [20]
#
Now, I want the main program to kill child after 20 seconds, irrespective of system time change.
With current code, if I manually change system time, child gets lesser or more time than 20 seconds allowance.
Example, when I change time backwards:
stdout: Wed 01 Jun 2022 04:14:44 PM IST hello [4]
stdout: Wed 01 Jun 2022 02:59:25 PM IST hello [5]
...
stdout: Wed 01 Jun 2022 02:59:41 PM IST hello [21]
stdout: Wed 01 Jun 2022 02:59:42 PM IST hello [22]
...
Found a related discussion here: https://bugs.python.org/issue31267
# python
Python 3.7.13 (default, May 27 2022, 08:39:11)
[GCC 8.4.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import sys
>>> sys.thread_info
sys.thread_info(name='pthread', lock='semaphore', version='NPTL 2.30')
>>>
I am on Linux, and lock used is semaphore, not mutex+cond.
What is the alternate mechanism that I can use to make my timer independent of system time change?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
该API支持超时,并且似乎不受系统时间更改影响。 https:///docs.python.org/3/library/subprocess。 html#subprocess.run
This API supports timeout, and it seems to work without being affected by system time change. https://docs.python.org/3/library/subprocess.html#subprocess.run