Python脚本异常和中断
我有一个运行另一个脚本的python脚本Python脚本异常和中断,python,python-3.x,Python,Python 3.x,我有一个运行另一个脚本的python脚本 inp = int(input('Do you want to continue ')) flag=1 while inp>0: if flag==1: print ('Script should run for detection!') exec(open("obj_d.py").read()) flag = 0 else: print('Scr
inp = int(input('Do you want to continue '))
flag=1
while inp>0:
if flag==1:
print ('Script should run for detection!')
exec(open("obj_d.py").read())
flag = 0
else:
print('Script should not run')
continue
我写这个脚本是为了在短时间内运行和关闭。这意味着当我输入1时,它应该运行这个脚本并自动关闭。但对于我的代码,它无法停止运行脚本。
那么,我该怎么做呢?(我不想在obj_d.py
文件中做任何更改。)您有什么其他建议吗
obj_d.py:
print('Here is your detection code ........................')
print('Started ...........')
while 1:
print('object detector...........')
print('Finished ..............')
我认为没有任何方法可以从调用脚本终止
exec
。如果要强制停止,应使用子流程
模块。比如:
import time
import subprocess
p = subprocess.Popen("python obj_d.py", shell=True)
time.sleep(4) # let it run for 4 seconds
p.kill() # terminate the process
为什么要从
a.py
运行b.py
。为什么不使用pythonimport
并使用b。py@balderman谢谢你的回复。如果我导入函数,许多库将需要再次导入,并且代码在任何时候都不能终止,这是主要问题。