subprocess.popen阻塞
时间: 2023-09-07 17:18:29 浏览: 264
subprocess.Popen在读取子进程的标准输出时,如果子进程没有输出,使用stdout.read()会导致程序阻塞。 为了避免阻塞,可以采用两种方法。一种是在读取输出之前,将文件描述符设置为非阻塞模式,例如使用fcntl库的fcntl函数进行设置。具体实现可以参考下面的代码示例:
```
import select
import os
import subprocess
import time
import fcntl
args = ['python','./fetch_file2.py',ip,path]
proc = subprocess.Popen(args, stdout=subprocess.PIPE,stderr=subprocess.PIPE,close_fds=True)
def non_block_read(output): # 避免阻塞
fd = output.fileno()
fl = fcntl.fcntl(fd, fcntl.F_GETFL)
fcntl.fcntl(fd, fcntl.F_SETFL, fl | os.O_NONBLOCK)
try:
return output.read()
except:
return ""
while proc.poll() is None: #fetch中rsync结束。但是fetch没有结束(怀疑输出过大) 导致这里一直是None
pass
print proc.poll() # 杀死fetch进程 返回-9
print proc.stderr.read() #阻塞#方法1:
#non_block_read(proc.stderr) #防止阻塞#方法2:
select_rfds = [ proc.stdout, proc.stderr]
(rfds, wfds, efds) = select.select(select_rfds, [],[])
if proc.stderr in rfds: #不存在。若select_rfds=[stderr],则阻塞在select上
len = proc.stderr.read(10)
if len == 0:
print "empty"
else:
print "proc.stderr"
if proc.stdout in rfds:
print "proc.stdout"
```
通过设置文件描述符为非阻塞模式,可以避免阻塞问题,并且使用select.select来检查子进程的标准输出是否可读。 这样即可解决subprocess.Popen阻塞的问题。<span class="em">1</span><span class="em">2</span><span class="em">3</span>
#### 引用[.reference_title]
- *1* [对Python subprocess.Popen子进程管道阻塞详解](https://download.csdn.net/download/weixin_38661128/12866393)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 33.333333333333336%"]
- *2* [python subprocess.Popen read阻塞问题解决](https://blog.csdn.net/weixin_41582874/article/details/127939185)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 33.333333333333336%"]
- *3* [python subprocess阻塞](https://blog.csdn.net/weixin_30855761/article/details/99389411)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 33.333333333333336%"]
[ .reference_list ]
阅读全文