gpt4 book ai didi

python - 监控程序崩溃

转载 作者:行者123 更新时间:2023-11-28 22:02:51 24 4
gpt4 key购买 nike

我目前正在使用 Python 模块 Spynner 来自动执行一些网络任务。我遇到了一个问题,尽管出于某种原因,该进程只是简单地停止移动、卡住,但仍会根据 Windows 进行响应。

我想做的是设置某种形式的监视器来检查并查看是否发生这种情况,然后重新启动该过程。我在想可能会监控程序的终端输出,如果它在一定时间后停止推送数据,它会杀死程序并重新启动。

我知道我想如何终止程序并再次运行它,只需使用 os 和子进程,但我不确定如何设置该程序以监视终端是否在特定时间内停止发送数据.

最佳答案

以下代码是从“Non-blocking read on a subprocess.PIPE in python”中借用并稍作修改的(J.F. Sebastian 的荣誉 - 如果您接受这个答案,请为原始代码点赞)

import sys
import time
from subprocess import PIPE, Popen
from threading import Thread

try:
from Queue import Queue, Empty
except ImportError:
from queue import Queue, Empty # python 3.x

ON_POSIX = 'posix' in sys.builtin_module_names

def enqueue_output(out, queue):
for line in iter(out.readline, b''):
timestamp = time.time( )
queue.put((timestamp, line))
out.close()

#-- This is how long you're willing to wait before you
#-- consider your Spynner process to be brain-dead.
MAX_WAIT_TIME = 300.0 #-- we'll wait 5 minutes (300 seconds)

#-- Construct a shared queue that will be used to send messages from
#-- the subprocess I/O polling thread to the watchdog (main) thread.
q = Queue()

#-- Spawn your subprocess...
p = Popen(['myprogram.exe'], stdout=PIPE, bufsize=1, close_fds=ON_POSIX)

#-- Create a new thread that runs in the same process as the watchdog.
#-- This thread will poll the output of the subprocess and populate the
#-- shared queue.
t = Thread(target=enqueue_output, args=(p.stdout, q))
t.daemon = True # thread dies with the program
t.start()

#-- Now, we'll try to read from the shared queue.
try:
#-- Queries the shared queue for the next item in the queue,
#-- waiting for up to MAX_WAIT_TIME before failing with an Empty exception.
timestamp, line = q.get(True, MAX_WAIT_TIME)
except Empty:
#-- Ok...the queue is empty and it's been MAX_WAIT_TIME since
#-- We've pulled anything from the queue.
p.terminate( ) #-- "terminate with extreme prejudice"
else: # got line
#-- Got a (timestamp, line_of_text) pair, where the timestamp is the
#-- system time when the I/O polling thread grabbed the line from
#-- the subprocess pipe. This timestamp isn't strictly necessary,
#-- but might come in handy in debugging the brain-dead Spynner process.
#-- So now...do something with that line of text!
doSomething(line)

您必须使用一些逻辑来扩充这段代码,以生成一个新的 Spynner 进程,以从终止的进程停止的地方继续,等等,但希望这能让您了解如何继续。

关于python - 监控程序崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10527273/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com