Python:timedout psutil进程在Windows中被终止(按照指示),但在Linux中没有

2024-07-02 13:17:39 发布

您现在位置:Python中文网/ 问答频道 /正文

这和我在Windows(64位Windows7HomePremium,SP1)上一样,使用Python3.5.1。 然而,在Linux(opensuse13.2,Harlequin,i586和kde4.14.9)上,使用python3.4.1,任何超时的进程都不会被杀死。在

我的处理过程基本上就是在StackOverflow上给出的答案 到Python: Run a process and kill it if it doesn't end within one hour (作者詹帕洛罗多拉,2012年5月10日)

以下是(简化)我所做的:

import os
import psutil


if os.name == 'nt':  # If running on Windows...
    app = r'C:\Program Files (x86)\Foxit Software\Foxit Reader\FoxitReader.exe'
else:
    app = r'apps/foxitreader/FoxitReader.sh'

process = psutil.Popen([app, os.path.join('raw_pdfs', 'Thinkpython.pdf')])

try:

    process.wait(timeout=5.0)  # Wait specified seconds to see if application crashes.

    print('Process crashed with return code %d.' % process.poll())
    # If you get here, the process crashed before timing out.

except psutil.TimeoutExpired:  # If the timeout expired as normally expected, Then...

    print('Process timed-out normally.')

    process.kill()

而不是FoxitReader进程在5秒后被终止,而是继续在FoxitReader中保持打开状态。在

产生的Python解释器输出是:

^{pr2}$

有时,输出还包括更多内容,似乎来自Qt(我认为FoxitReader的Linux版本是用Qt编写的)。 我不认为这是相关的,但是(万一我错了)here就是一个例子。在

我试着做:

process.terminate()

在此之前:

process.kill()

(看起来How to kill Linux process with Python可能暗示了这一点,但这没什么区别。在

[这是一些Python3对PDF阅读器的“模糊测试”。 我在有效的PDF文件中随机更改一些字节,然后测试是否有任何“模糊化”文件使任何PDF阅读器崩溃。 偶尔也会导致其中一个崩溃。]


Tags: importappifpdf进程oslinuxwindows
2条回答

哎呀!我没有意识到。在

与Windows、Linux(或者至少在opensuse13.2、Harlequin、使用python3.4.1的kde4.14.9的i586上)不同,FoxitReader在一个子进程中运行,因此也需要终止。在

我能够按照jung rhew 2014年11月20日对StackOverflow问题的回答how to kill process and child processes from python?中的指示完成了这项工作

也许你应该指定终止代码。根据文档,kill()方法接受参数。尝试使用p.kill(pid=your_pid_num,9),因为它显示“在UNIX上,这与os.杀死(pid,SIGKILL信号).' https://pythonhosted.org/psutil/#psutil.Process.kill

相关问题 更多 >