import subprocesswhile True: proc = subprocess.Popen(["python3", "/home/pi/Documents/8-17/simplest.py"]) proc.wait() # 等待子进程执行完毕 if proc.returncode == 0: print("simplest运行正常.....") break simplest执行后终端一直打印simplest的输出而不会打印if判断里的输出,simplest是一个不会运行结束的程序
时间: 2023-12-10 18:38:39 浏览: 30
这是因为 `Popen` 函数启动的子进程在执行 `simplest.py` 时一直在输出信息,导致主进程一直等待子进程结束,无法执行后续代码。您可以使用 `communicate` 方法,在子进程执行结束后获取其输出,或者将子进程的输出重定向到文件中,避免输出影响主进程。例如:
```python
import subprocess
while True:
proc = subprocess.Popen(["python3", "/home/pi/Documents/8-17/simplest.py"], stdout=subprocess.PIPE)
out, err = proc.communicate() # 获取子进程的输出
if proc.returncode == 0:
print("simplest运行正常.....")
break
```
或者:
```python
import subprocess
while True:
with open('simplest_output.txt', 'w') as f:
proc = subprocess.Popen(["python3", "/home/pi/Documents/8-17/simplest.py"], stdout=f)
proc.wait() # 等待子进程执行完毕
if proc.returncode == 0:
print("simplest运行正常.....")
break
```
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![xlsx](https://img-home.csdnimg.cn/images/20210720083732.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)