失眠网,内容丰富有趣,生活中的好帮手!
失眠网 > 有两个python怎么停用其中一_如何在python中停止另一个已经运行的脚本?

有两个python怎么停用其中一_如何在python中停止另一个已经运行的脚本?

时间:2023-03-21 02:04:22

相关推荐

有两个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中停止另一个已经运行的脚本?》对你有帮助,请点赞、收藏,并留下你的观点哦!

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。