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 11:38:40 浏览: 35
可以使用 `subprocess.Popen` 的 `stdout` 和 `stderr` 参数将子进程的输出重定向到文件中,这样就不会在终端上打印子进程输出了。修改代码如下:
```python
import subprocess
while True:
with open('/home/pi/Documents/8-17/simplest_output.txt', 'w') as f:
proc = subprocess.Popen(["python3", "/home/pi/Documents/8-17/simplest.py"], stdout=f, stderr=f)
proc.wait() # 等待子进程执行完毕
if proc.returncode == 0:
print("simplest运行正常.....")
break
```
这里将子进程的标准输出和标准错误输出都重定向到了同一个文件 `/home/pi/Documents/8-17/simplest_output.txt` 中,如果子进程运行时间过长,可以通过查看该文件来了解子进程的输出情况。
相关问题
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是一个不会运行结束的程序
这是因为 `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
```
import subprocess while True: proc = subprocess.Popen(["python3", "/home/pi/Documents/8-17/simplest.py"]) try: while True: if proc.poll() is not None: break print("simplest运行成功.....") except KeyboardInterrupt: proc.terminate() break 这样simplest死循环里打印的信息不会出现在终端
这是因为 `subprocess.Popen` 创建的子进程是在独立的进程空间内执行的,并不会把输出打印到终端。要想在终端打印子进程的输出,可以使用 `subprocess.PIPE` 来捕获子进程的标准输出和标准错误输出,然后在父进程中打印出来。修改代码如下:
```python
import subprocess
while True:
proc = subprocess.Popen(["python3", "/home/pi/Documents/8-17/simplest.py"],
stdout=subprocess.PIPE, stderr=subprocess.PIPE)
try:
while True:
if proc.poll() is not None:
break
# 读取子进程的输出
output = proc.stdout.readline().decode().strip()
if output:
print(output)
error = proc.stderr.readline().decode().strip()
if error:
print(error)
except KeyboardInterrupt:
proc.terminate()
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)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![pptx](https://img-home.csdnimg.cn/images/20210720083543.png)
![txt](https://img-home.csdnimg.cn/images/20210720083642.png)