Python中的单调监管计时器

发布于 2025-02-03 05:57:05 字数 1684 浏览 2 评论 0原文

我有一个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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

静谧 2025-02-10 05:57:05

该API支持超时,并且似乎不受系统时间更改影响。 https:///docs.python.org/3/library/subprocess。 html#subprocess.run

import subprocess

try:
    proc = subprocess.run(['./plugin.sh'],
            stdout=subprocess.PIPE,
            stderr=subprocess.PIPE,
            universal_newlines=True,
            timeout=10)
    for line in proc.stdout.splitlines():
        print('Plugin stdout: ' + line)

except subprocess.TimeoutExpired as t:
    for line in t.stdout.decode().splitlines():
        print('Plugin stdout: ' + line)
    print('Timed out')

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

import subprocess

try:
    proc = subprocess.run(['./plugin.sh'],
            stdout=subprocess.PIPE,
            stderr=subprocess.PIPE,
            universal_newlines=True,
            timeout=10)
    for line in proc.stdout.splitlines():
        print('Plugin stdout: ' + line)

except subprocess.TimeoutExpired as t:
    for line in t.stdout.decode().splitlines():
        print('Plugin stdout: ' + line)
    print('Timed out')

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文