[英]How to exit Python Script from Terminal
so I am trying to run a python script from terminal and at the end it does not exit the process unit I hit ENTER
. 所以我试图从终端运行python脚本,最后它没有退出我按
ENTER
的进程单元。 I tried to kill the process and use the following, sys.exit()
, quit()
, raise SystemExit
but nothing seem to work. 我试图杀死该进程,并使用以下
sys.exit()
, quit()
, raise SystemExit
但似乎没有任何效果。 Below is the code I am using. 下面是我正在使用的代码。 Any ideas please?
有什么想法吗?
# -*- coding: utf-8 -*-
import subprocess, sys, os
from sys import exit
firstarg=sys.argv[1]
service_provider = subprocess.Popen (['ffprobe', '-loglevel', 'fatal', '-select_streams', '-show_entries', 'program_tags', '-of', 'csv=%s' % ("p=0"), '-i', sys.argv[1]], stdout=subprocess.PIPE)
out = service_provider.communicate() [0]
result = out.split('\n')
a = str(result [0])
b = str(result [1])
cmds1 = ['ffmpeg', '-i', sys.argv[1], '-c', 'copy', '-out1.ts']
cmds2 = ['ffmpeg', '-i', sys.argv[1], '-c', 'copy', '-out2.ts']
subprocess.Popen(cmds1)
subprocess.Popen(cmds2)
sys.exit()
I had similar problem when worked with subprocess.Popen, if your Popen subprocessess are not finished it can cause problem. 使用subprocess.Popen时,我遇到了类似的问题,如果您的Popen子过程未完成,可能会导致问题。
popenlist in the example is dictionary {'anynameyouchoose:subprocess.Popen()'} 示例中的popenlist是字典{'anynameyouchoose:subprocess.Popen()'}
def exit_program(popenlist):
if len(popenlist):
for activepopen in popenlist.values():
activepopen.kill()
print("The program will be terminated in 5 seconds.\n")
time.sleep(4)
print("Bye...\n")
time.sleep(1)
sys.exit()
okay, so I managed to get it working by adding the following to the code, 好的,所以我设法通过在代码中添加以下内容来使其正常工作,
out = cmds1.communicate()
cmds1.kill()
not sure if this is the clean way of doing. 不知道这是否是干净的方法。 Its doing the job but I am getting the following error at the end just before it exits,
它可以完成工作,但是在退出之前,我最终收到以下错误,
Traceback (most recent call last):
File "pytest8.py", line 23, in <module>
cmds1.kill()
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/subprocess.py", line 1564, in kill
self.send_signal(signal.SIGKILL)
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/subprocess.py", line 1554, in send_signal
os.kill(self.pid, sig)
OSError: [Errno 3] No such process
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.