[英]How do you tell whether sys.stdin.readline() is going to block?
如何確定對 sys.stdin.readline() (或更一般地說,任何基於文件描述符的文件對象上的 readline() )的調用是否會阻塞?
當我在 python 中編寫基於行的文本過濾程序時會出現這種情況; 也就是說,程序反復從輸入中讀取一行文本,可能對其進行轉換,然后將其寫入輸出。
我想實現一個合理的輸出緩沖策略。 我的標准是:
因此,無緩沖輸出是不好的,因為它違反了 (1)(對操作系統的寫入過多)。 行緩沖輸出也不好,因為它仍然違反 (1)(將輸出批量刷新到操作系統中的每一百萬行是沒有意義的)。 默認緩沖輸出也不好,因為它違反了 (2)(如果輸出到文件或管道,它將不適當地保留輸出)。
我認為在大多數情況下,一個好的解決方案是:“每當(其緩沖區已滿或)sys.stdin.readline() 即將阻塞時,就刷新 sys.stdout”。 可以實施嗎?
(注意,我並沒有聲稱這個策略對所有情況都是完美的。例如,在程序受 CPU 限制的情況下它可能並不理想;在這種情況下,更頻繁地刷新可能是明智的,以避免在進行長時間的計算。)
為了確定性,假設我正在 python 中實現 unix 的“cat -n”程序。
(實際上,“cat -n”比一次一行更聰明;也就是說,它知道如何在讀取整行之前讀取和寫入一行的一部分;但是,對於這個例子,我要無論如何都要一次一行地實現它。)
(表現良好,但違反了標准(1),即由於沖洗過多而慢得不合理):
#!/usr/bin/python
# cat-n.linebuffered.py
import sys
num_lines_read = 0
while True:
line = sys.stdin.readline()
if line == '': break
num_lines_read += 1
print("%d: %s" % (num_lines_read, line))
sys.stdout.flush()
(快速但違反標准(2),即不友好的輸出扣留)
#!/usr/bin/python
# cat-n.defaultbuffered.py
import sys
num_lines_read = 0
while True:
line = sys.stdin.readline()
if line == '': break
num_lines_read += 1
print("%d: %s" % (num_lines_read, line))
#!/usr/bin/python
num_lines_read = 0
while True:
if sys_stdin_readline_is_about_to_block(): # <--- How do I implement this??
sys.stdout.flush()
line = sys.stdin.readline()
if line == '': break
num_lines_read += 1
print("%d: %s" % (num_lines_read, line))
所以問題是:是否可以實現sys_stdin_readline_is_about_to_block()
?
我想要一個在 python2 和 python3 中都有效的答案。 我已經研究了以下每一種技術,但到目前為止還沒有任何結果。
使用select([sys.stdin],[],[],0)
確定從 sys.stdin 讀取是否會阻塞。 (當 sys.stdin 是緩沖文件對象時,這不起作用,至少有一個或兩個原因:(1)如果部分行准備好從底層輸入管道讀取,它會錯誤地說“不會阻塞”, (2) 如果 sys.stdin 的緩沖區包含一個完整的輸入行,但底層管道還沒有准備好進行額外的讀取,它會錯誤地說“將阻塞”......我認為)。
非阻塞 io,使用os.fdopen(sys.stdin.fileno(), 'r')
和fcntl
和O_NONBLOCK
(我無法讓它在任何 python 版本中與 readline() 一起使用:在 python2.7 中,它丟失了每當有部分行進入時輸入;在 python3 中,似乎無法區分“會阻塞”和輸入結束。??)
asyncio (我不清楚這在 python2 中有什么可用;我認為它不適用於 sys.stdin;但是,我仍然對僅在從子進程返回的管道中讀取時才有效的答案感興趣.Popen())。
創建一個線程來執行readline()
循環並通過 queue.Queue 將每一行傳遞給主程序; 然后主程序可以在從中讀取每一行之前輪詢隊列,並且每當它看到它即將阻塞時,首先刷新標准輸出。 (我試過這個,實際上讓它工作了,見下文,但它非常慢,比行緩沖慢得多。)
請注意,這並沒有嚴格回答“如何判斷 sys.stdin.readline() 是否會阻塞”的問題,但它無論如何都設法實現了所需的緩沖策略。 不過太慢了。
#!/usr/bin/python
# cat-n.threaded.py
import queue
import sys
import threading
def iter_with_abouttoblock_cb(callable, sentinel, abouttoblock_cb, qsize=100):
# child will send each item through q to parent.
q = queue.Queue(qsize)
def child_fun():
for item in iter(callable, sentinel):
q.put(item)
q.put(sentinel)
child = threading.Thread(target=child_fun)
# The child thread normally runs until it sees the sentinel,
# but we mark it daemon so that it won't prevent the parent
# from exiting prematurely if it wants.
child.daemon = True
child.start()
while True:
try:
item = q.get(block=False)
except queue.Empty:
# q is empty; call abouttoblock_cb before blocking
abouttoblock_cb()
item = q.get(block=True)
if item == sentinel:
break # do *not* yield sentinel
yield item
child.join()
num_lines_read = 0
for line in iter_with_abouttoblock_cb(sys.stdin.readline,
sentinel='',
abouttoblock_cb=sys.stdout.flush):
num_lines_read += 1
sys.stdout.write("%d: %s" % (num_lines_read, line))
以下命令(在 linux 上的 bash 中)顯示了預期的緩沖行為:“defaultbuffered”緩沖過於激進,而“linebuffered”和“threaded”緩沖恰到好處。
(請注意,管道末尾的| cat
是默認情況下制作 python 塊緩沖區而不是行緩沖區。)
for which in defaultbuffered linebuffered threaded; do
for python in python2.7 python3.5; do
echo "$python cat-n.$which.py:"
(echo z; echo -n a; sleep 1; echo b; sleep 1; echo -n c; sleep 1; echo d; echo x; echo y; echo z; sleep 1; echo -n e; sleep 1; echo f) | $python cat-n.$which.py | cat
done
done
輸出:
python2.7 cat-n.defaultbuffered.py:
[... pauses 5 seconds here. Bad! ...]
1: z
2: ab
3: cd
4: x
5: y
6: z
7: ef
python3.5 cat-n.defaultbuffered.py:
[same]
python2.7 cat-n.linebuffered.py:
1: z
[... pauses 1 second here, as expected ...]
2: ab
[... pauses 2 seconds here, as expected ...]
3: cd
4: x
5: y
6: z
[... pauses 2 seconds here, as expected ...]
6: ef
python3.5 cat-n.linebuffered.py:
[same]
python2.7 cat-n.threaded.py:
[same]
python3.5 cat-n.threaded.py:
[same]
(在 Linux 上的 bash 中):
for which in defaultbuffered linebuffered threaded; do
for python in python2.7 python3.5; do
echo -n "$python cat-n.$which.py: "
timings=$(time (yes 01234567890123456789012345678901234567890123456789012345678901234567890123456789 | head -1000000 | $python cat-n.$which.py >| /tmp/REMOVE_ME) 2>&1)
echo $timings
done
done
/bin/rm /tmp/REMOVE_ME
輸出:
python2.7 cat-n.defaultbuffered.py: real 0m1.490s user 0m1.191s sys 0m0.386s
python3.5 cat-n.defaultbuffered.py: real 0m1.633s user 0m1.007s sys 0m0.311s
python2.7 cat-n.linebuffered.py: real 0m5.248s user 0m2.198s sys 0m2.704s
python3.5 cat-n.linebuffered.py: real 0m6.462s user 0m3.038s sys 0m3.224s
python2.7 cat-n.threaded.py: real 0m25.097s user 0m18.392s sys 0m16.483s
python3.5 cat-n.threaded.py: real 0m12.655s user 0m11.722s sys 0m1.540s
重申一下,我想要一個解決方案,它在保持緩沖輸出時從不阻塞(“linebuffered”和“threaded”在這方面都很好),而且速度也很快:也就是說,速度與“defaultbuffered”相當。
你當然可以使用select
:這就是它的用途,它的性能對於少量的文件描述符來說是好的。 您必須自己實現行緩沖/中斷,以便您可以檢測在緩沖(結果是)部分行后是否有更多可用輸入。
您可以自己進行所有緩沖(這是合理的,因為select
在文件描述符級別運行),或者您可以將stdin
設置為非阻塞並使用file.read()
或BufferedReader.read()
(取決於您的Python 版本)來使用任何可用的東西。 如果您的輸入可能是 Internet 套接字,則無論緩沖如何,您都必須使用非阻塞輸入,因為select
常見實現可能會虛假地指示來自套接字的可讀數據。 (在這種情況下,Python 2 版本使用EAGAIN
引發IOError
;Python 3 版本返回None
。)
( os.fdopen
在這里沒有幫助,因為它不會創建新的文件描述符供fcntl
使用。在某些系統上,您可以使用O_NONBLOCK
打開/dev/stdin
。)
基於默認(緩沖) file.read()
Python 2 實現:
import sys,os,select,fcntl,errno
fcntl.fcntl(sys.stdin.fileno(),fcntl.F_SETFL,os.O_NONBLOCK)
rfs=[sys.stdin.fileno()]
xfs=rfs+[sys.stdout.fileno()]
buf=""
lnum=0
timeout=None
rd=True
while rd:
rl,_,xl=select.select(rfs,(),xfs,timeout)
if xl: raise IOError # "exception" occurred (TCP OOB data?)
if rl:
try: rd=sys.stdin.read() # read whatever we have
except IOError as e: # spurious readiness?
if e.errno!=errno.EAGAIN: raise # die on other errors
else: buf+=rd
nl0=0 # previous newline
while True:
nl=buf.find('\n',nl0)
if nl<0:
buf=buf[nl0:] # hold partial line for "processing"
break
lnum+=1
print "%d: %s"%(lnum,buf[nl0:nl])
timeout=0
nl0=nl+1
else: # no input yet
sys.stdout.flush()
timeout=None
if buf: sys.stdout.write("%d: %s"%(lnum+1,buf)) # write any partial last line
對於cat -n
,我們可以在獲得它們后立即寫出部分行,但這會保留它們以表示一次處理整行。
在我的(不起眼的)機器上,您的yes
測試需要“真正的 0m2.454s 用戶 0m2.144s sys 0m0.504s”。
# -*- coding: utf-8 -*-
import os
import sys
import select
import fcntl
import threading
class StdInput:
def __init__(self):
self.close_evt = threading.Event()
fcntl.fcntl(sys.stdin.fileno(), fcntl.F_SETFL, fcntl.fcntl(sys.stdin.fileno(), fcntl.F_GETFL) | os.O_NONBLOCK);
self.input = (sys.stdin.original_stdin if hasattr(sys.stdin, "original_stdin") else sys.stdin)
self.epoll = select.epoll()
self.epoll.register(sys.stdin.fileno(), select.EPOLLIN | select.EPOLLPRI | select.EPOLLERR | select.EPOLLHUP | select.EPOLLRDBAND)
def read(self):
while not self.close_evt.is_set():
input_line = self.input.readline()
# If the object is in non-blocking mode and no bytes are available, None is returned.
if input_line is not None and len(input_line) > 0:
break
print("Nothing yet...")
evt_lst = self.epoll.poll(1.0) # Timeout 1s
print("Poll exited: event list size={}".format(len(evt_lst)))
if len(evt_lst) > 0:
assert len(evt_lst) == 1
if (evt_lst[0][1] & (select.EPOLLERR | select.EPOLLHUP)) > 0:
raise Exception("Ooops!!!")
return input_line
if __name__ == "__main__":
i = StdInput()
def alm_handle():
i.close_evt.set()
threading.Timer(4, alm_handle).start()
print("Reading...")
input_line = i.read()
print("Read='{}'".format(input_line))
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.