从另一个 shell 中的另一个 Python 脚本打开一个 Python 脚本

Open a Python script from another Python script in another shell

我目前正在研究 Raspbian。我的问题是我有一个 Python 带有无限循环且永远不会停止的凭据。在这个脚本中,我想在主脚本不停止的情况下调用另一个脚本。我尝试了不同的方法来做到这一点:

import test1
test1.some_func()

execfile("test1.py")

import subprocess
subprocess.call("python test1.py")

我可以使用这些解决方案启动 test1.py 脚本,但调用它的脚本会停止工作。所以我的问题是如何在第一个脚本停止的情况下启动第二个脚本。

subprocess.call 等待命令完成,从而阻止循环。你应该使用类似 process = subprocess.Popen(["python", "test1.py"]) 的东西。如果你想等待进程终止,你可以调用 process.wait().