在 Python 中终止一个程序
Killing a program in Python
我有一个循环,我 运行 一个已安装的程序使用
os.system("program + arguments")
将循环中的每个元素作为参数传递给程序。程序的运行时间根据参数而变化,有时需要一秒钟,有时需要几个小时。所以我想在一个多小时的时候杀掉这个程序,然后继续循环中的下一个元素。
我在这里尝试使用第二个答案(因为我不明白如何使用最佳答案)Python: Run a process and kill it if it doesn't end within one hour 将 os.sytem("program+arguments")
替换为 subprocess.Popen(["program+arguments"])
但它给出了 "No such file or directory error"
,我确定我正确地传递了参数,你能帮我如何应用这样的解决方案吗?
这是错误信息,
subp = subprocess.Popen(["home/admin/Desktop/molblocks/fragment -i " + OrgDir+"/"+compsmi + " -r "+ RulesPath + " -e -n "+str(FragLength) + " -o " + compsmi + "_frag.txt"])
File "/usr/lib64/python2.7/subprocess.py", line 709, in __init__
errread, errwrite)
File "/usr/lib64/python2.7/subprocess.py", line 1326, in _execute_child
raise child_exception
此致!
您可以像这样设置一个计时器来终止进程
import subprocess
import threading
proc = subprocess.call("program + arguments", shell=True)
timer = threading.Timer(3600, proc.kill)
timer.cancel()
proc.wait()
call
、check_call
和Popen
调用接受程序的两种形式。应传递给子 shell 的单个字符串 ("program arg1 arg2")(您需要 shell=True
才能使其工作)或参数列表 ["program"、"arg1"、"arg2"](shell=True
是可选的,如果不使用子 shell,linux 会更有效)。您将程序 + args 作为单个项目放入列表中,python 尽职尽责地转义了所有分隔符并尝试 运行 一个全名的程序。
在 Unix 上,如果 args 是一个字符串,则该字符串被解释为要执行的程序的名称或路径。但是,这只有在不向程序传递参数的情况下才能完成。 (当 shell 不为真时)
https://docs.python.org/2/library/subprocess.html#subprocess.Popen.
试试这个:
subp = subprocess.Popen(["home/admin/Desktop/molblocks/fragment", "-i", (OrgDir+"/"+compsmi), "-r", RulesPath, "-e", "-n", str(FragLength), "-o", (compsmi+"_frag.txt")])
我有一个循环,我 运行 一个已安装的程序使用
os.system("program + arguments")
将循环中的每个元素作为参数传递给程序。程序的运行时间根据参数而变化,有时需要一秒钟,有时需要几个小时。所以我想在一个多小时的时候杀掉这个程序,然后继续循环中的下一个元素。
我在这里尝试使用第二个答案(因为我不明白如何使用最佳答案)Python: Run a process and kill it if it doesn't end within one hour 将 os.sytem("program+arguments")
替换为 subprocess.Popen(["program+arguments"])
但它给出了 "No such file or directory error"
,我确定我正确地传递了参数,你能帮我如何应用这样的解决方案吗?
这是错误信息,
subp = subprocess.Popen(["home/admin/Desktop/molblocks/fragment -i " + OrgDir+"/"+compsmi + " -r "+ RulesPath + " -e -n "+str(FragLength) + " -o " + compsmi + "_frag.txt"])
File "/usr/lib64/python2.7/subprocess.py", line 709, in __init__
errread, errwrite)
File "/usr/lib64/python2.7/subprocess.py", line 1326, in _execute_child
raise child_exception
此致!
您可以像这样设置一个计时器来终止进程
import subprocess
import threading
proc = subprocess.call("program + arguments", shell=True)
timer = threading.Timer(3600, proc.kill)
timer.cancel()
proc.wait()
call
、check_call
和Popen
调用接受程序的两种形式。应传递给子 shell 的单个字符串 ("program arg1 arg2")(您需要 shell=True
才能使其工作)或参数列表 ["program"、"arg1"、"arg2"](shell=True
是可选的,如果不使用子 shell,linux 会更有效)。您将程序 + args 作为单个项目放入列表中,python 尽职尽责地转义了所有分隔符并尝试 运行 一个全名的程序。
在 Unix 上,如果 args 是一个字符串,则该字符串被解释为要执行的程序的名称或路径。但是,这只有在不向程序传递参数的情况下才能完成。 (当 shell 不为真时)
https://docs.python.org/2/library/subprocess.html#subprocess.Popen.
试试这个:
subp = subprocess.Popen(["home/admin/Desktop/molblocks/fragment", "-i", (OrgDir+"/"+compsmi), "-r", RulesPath, "-e", "-n", str(FragLength), "-o", (compsmi+"_frag.txt")])