Python:超时的 psutil 进程在 Windows 中被杀死(按照指示),但在 Linux 中没有
Python: timed-out psutil process killed (as instructed) in Windows, but not in Linux
这在 Windows(64 位 Windows 7 Home Premium,SP1)上使用 Python 3.5.1 时如我所料。
但是,在 Linux(OpenSUSE 13.2、Harlequin、i586 和 KDE 4.14.9)上,使用 Python 3.4.1,任何超时进程都不会被杀死。
我的流程处理基本上是Whosebug上给出的答案
至 Python: Run a process and kill it if it doesn't end within one hour
(作者:Giampaolo Rodolà,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 秒后被终止,PDF 文件继续在 FoxitReader 中保持打开状态。
最终的 Python 解释器输出是:
Openfile()---fileName-: "raw_pdfs/Thinkpython.pdf"
sendMessage
Process timed-out normally.
有时,输出还包含更多内容,似乎来自 Qt(我认为 Linux 版本的 FoxitReader 就是用它编写的)。
我认为这无关紧要,但是(以防万一我错了)here 是一个例子。
我试过:
process.terminate()
之前:
process.kill()
(看起来 How to kill Linux process with Python 可能是在暗示,但这没有区别。
[这适用于一些 Python3 'fuzz testing' 的 PDF 阅读器。
我随机更改有效 PDF 文件中的一些字节,然后测试是否有任何 'fuzzed' 文件使任何 PDF 阅读器崩溃。
它偶尔会导致其中 1 个崩溃。]
也许您应该指定终止代码。根据文档, kill() 方法接受参数。尝试 p.kill(pid=your_pid_num, 9),因为它说 'on UNIX this is the same as os.kill(pid, signal.SIGKILL).'
https://pythonhosted.org/psutil/#psutil.Process.kill
糟糕!没想到
不同于 Windows,Linux(或至少在 OpenSUSE 13.2、Harlequin、i586 和 KDE 4.14.9,使用 Python 3.4.1),FoxitReader 获得 运行 在子进程中,因此也需要被杀死。
我能够按照 jung rhew 2014 年 11 月 20 日对 Whosebug 问题的回答中的指示做到这一点 how to kill process and child processes from python?
这在 Windows(64 位 Windows 7 Home Premium,SP1)上使用 Python 3.5.1 时如我所料。 但是,在 Linux(OpenSUSE 13.2、Harlequin、i586 和 KDE 4.14.9)上,使用 Python 3.4.1,任何超时进程都不会被杀死。
我的流程处理基本上是Whosebug上给出的答案 至 Python: Run a process and kill it if it doesn't end within one hour (作者:Giampaolo Rodolà,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 秒后被终止,PDF 文件继续在 FoxitReader 中保持打开状态。
最终的 Python 解释器输出是:
Openfile()---fileName-: "raw_pdfs/Thinkpython.pdf"
sendMessage
Process timed-out normally.
有时,输出还包含更多内容,似乎来自 Qt(我认为 Linux 版本的 FoxitReader 就是用它编写的)。 我认为这无关紧要,但是(以防万一我错了)here 是一个例子。
我试过:
process.terminate()
之前:
process.kill()
(看起来 How to kill Linux process with Python 可能是在暗示,但这没有区别。
[这适用于一些 Python3 'fuzz testing' 的 PDF 阅读器。 我随机更改有效 PDF 文件中的一些字节,然后测试是否有任何 'fuzzed' 文件使任何 PDF 阅读器崩溃。 它偶尔会导致其中 1 个崩溃。]
也许您应该指定终止代码。根据文档, kill() 方法接受参数。尝试 p.kill(pid=your_pid_num, 9),因为它说 'on UNIX this is the same as os.kill(pid, signal.SIGKILL).' https://pythonhosted.org/psutil/#psutil.Process.kill
糟糕!没想到
不同于 Windows,Linux(或至少在 OpenSUSE 13.2、Harlequin、i586 和 KDE 4.14.9,使用 Python 3.4.1),FoxitReader 获得 运行 在子进程中,因此也需要被杀死。
我能够按照 jung rhew 2014 年 11 月 20 日对 Whosebug 问题的回答中的指示做到这一点 how to kill process and child processes from python?