有两个python怎么停用其中一_如何在python中停止另一个已经运行的脚本?
There is a way to start another script in python by doing this:
import os
os.system("python [name of script].py")
So how can i stop another already running script? I would like to stop the script by using the name.
解決方案
It is more usual to import the other script and then invoke its functions and methods.
If that does not work for you, e.g. the other script is not written in such a way that is conducive to being imported, then you can use the subprocess module to start another process.
import subprocess
p = subprocess.Popen(['python', 'script.py', 'arg1', 'arg2'])
# continue with your code then terminate the child
p.terminate()
There are many possible ways to control and interact with the child process, e.g. you can can capture its stdout and sterr, and send it input. See the Popen() documentation.
總結
以上是生活随笔為你收集整理的有两个python怎么停用其中一_如何在python中停止另一个已经运行的脚本?的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Python存储生成的决策树——pick
- 下一篇: 的图片怎么循环渲染_十分钟教你做个炫酷的