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 我想在子进程运行中,主进程循环打印“子进程运行成功”,不是在子进程运行结束后打印
时间: 2023-12-10 16:39:04 浏览: 24
你可以通过在主进程中使用`threading`模块来实现此功能。具体代码如下:
```python
import subprocess
import threading
def run_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("子进程运行成功.....")
def main():
t = threading.Thread(target=run_subprocess)
t.start()
while True:
# 主进程循环执行其他操作
pass
if __name__ == '__main__':
main()
```
在这个代码中,`run_subprocess()`函数是一个无限循环的子线程,它会一直执行子进程,并在子进程运行成功后打印一条消息。在`main()`函数中,我们创建了一个新的子线程来运行`run_subprocess()`函数,同时主线程也在循环执行其他操作。这样就可以在子进程运行时不断地打印消息了。
相关推荐
![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)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![gz](https://img-home.csdnimg.cn/images/20210720083447.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)
![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)