简体   繁体   English

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

[英]Open a Python script from another Python script in another shell

I am working currently on Raspbian. 我目前正在研究Raspbian。 My Problem is i have a Python scrip with a infinite Loop that never should stop. 我的问题是我有一个带有无限循环的Python脚本,永远不要停止。 In This script i want to call another script without the main script stopping. 在此脚本中,我想在不停止主脚本的情况下调用另一个脚本。 I tried different methodes to do this like: 我尝试了不同的方法来做到这一点,例如:

import test1
test1.some_func()

or 要么

execfile("test1.py")

or 要么

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

I could start the test1.py script with these solutions, but the script that called it would stop working. 我可以使用这些解决方案启动test1.py脚本,但是调用该脚本的脚本将停止工作。 So my question is how to start a second script without the first one to stop. 所以我的问题是如何在不停止第一个脚本的情况下启动第二个脚本。

subprocess.call waits for the command to complete and thus blocks your loop. subprocess.call等待命令完成,从而阻塞了循环。 You should use something like process = subprocess.Popen(["python", "test1.py"]) . 您应该使用类似process = subprocess.Popen(["python", "test1.py"]) If you want to wait for the process to terminate, you can then call process.wait() . 如果要等待进程终止,则可以调用process.wait()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM