使用check_parent_select
時,在閱讀器側關閉後,未填寫例外列表。選擇不適用於python管道?
但是使用check_parent_poll
,在閱讀器側關閉後,它可以檢測到管道斷開。
有人知道根本原因嗎?
#!/usr/bin/python2.7
import select
import sys
import os
log=open("./test.log","w")
(reader, writer) = os.pipe()
def check_parent_select(fh):
(rlist, wlist, xlist) = select.select([], [], [fh], 1)
if fh in xlist:
print "parent exit"
else:
print "parent OK"
def check_parent_poll(fh):
poller = select.poll()
EVENTS = select.POLLERR
poller.register(fh)
events = poller.poll()
for fd, flag in events:
if flag & select.POLLERR:
print "parent exit"
else:
print "parent OK"
open_file = os.fdopen(writer, "w")
check_parent_select(open_file)
os.close(reader)
check_parent_select(open_file)
使用strace追蹤選擇功能,選擇無法檢測管道關閉。
管([4,5])= 0
選擇(6,[],[],[5],{1,0})= 0(超時)
寫(1, 「父行\ n」 個,10parent行
接近(4)= 0
選擇(6,[],[],[5],{1,0})= 0(超時)
這是從經驗中也可以指向規範呢?因爲關閉另一端觸發讀事件似乎有點奇怪。 – dhke
關閉管道的讀取結束不是錯誤,因此在「x」列表中將不會顯示等待的通知。例如,通過輪詢系統調用,管道末端的關閉通過返回讀取事件列表中的POLLHUP來發出信號。 – user237419
但我想知道套接字行爲是否與此一致? – jaslip