0

我是 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.

谢谢你的建议。

4

1 回答 1

0

对于通过终端运行的进程,请查看进程模块。要等待 5 秒,请使用时间模块。等待后,您可以以读取模式打开文件,然后读取直到找到“退出”。一个简单的例子:

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()
于 2013-07-05T07:04:59.757 回答