我是python中的新手,所以我无法在以下场景中创建python脚本:
1. Run a sub process.
2. Need to check in every 5 sec a file stop.txt is exit then
3. Stop the subrocess and exit from the script.
4. If not exit the stop.txt. then need to continue the sub process.
先谢谢。
答案 0 :(得分:0)
要通过终端运行流程,请查看subprocess模块。要等待5秒钟,请使用time模块。等待后,您可以在读取模式下打开文件,然后读取,直到找到“退出”。一个简单的例子:
import subproccess, time
background_task = subproccess.Popen(background_task)
while 1:
time.sleep(5)
with open('background_task.txt', 'r') as task_file:
if task_file.read() == 'exit':
background_task.kill()
exit()