如何确定调用sys.stdin.readline()(或更一般地说,任何基于文件描述符的文件对象上的readline())是否会阻塞?
当我在python中编写基于行的文本过滤程序时,这个问题就出现了; 也就是说,该程序重复从输入读取一行文本,可能对其进行转换,然后将其写入输出。
我想实现一个合理的输出缓冲策略。 我的标准是:
1. 在处理数百万行数据时,它应该是高效的-大多数情况下缓冲输出,偶尔刷新。 2. 在保持缓冲输出时不应阻塞输入。
因此,非缓冲输出不好,因为它违反了(1)(向操作系统写入太多次)。 而行缓冲输出也不好,因为它仍然违反了(1)(在批量处理一百万行中的每一行时将输出刷新到操作系统没有意义)。 默认缓冲输出也不好,因为它违反了(2)(如果输出是文件或管道,则会不适当地保留输出)。
我认为,在大多数情况下,一个好的解决方案是: “每当(其缓冲区已满或)sys.stdin.readline()即将阻塞时,刷新sys.stdout”。 可以实现吗?
(请注意,我不断言这种策略对于所有情况都是完美的。例如, 在程序受CPU限制的情况下,它可能不理想;在这种情况下,更频繁地刷新以避免在执行长时间计算时保留输出可能是明智的。)
为了明确起见,让我们说我正在使用python实现unix的“cat -n”程序。
所以问题是:是否可以实现
我希望得到适用于Python2和Python3的答案。 我已经研究了以下每种技术,但迄今为止都无济于事。
使用
使用
创建一个线程来执行
当我在python中编写基于行的文本过滤程序时,这个问题就出现了; 也就是说,该程序重复从输入读取一行文本,可能对其进行转换,然后将其写入输出。
我想实现一个合理的输出缓冲策略。 我的标准是:
1. 在处理数百万行数据时,它应该是高效的-大多数情况下缓冲输出,偶尔刷新。 2. 在保持缓冲输出时不应阻塞输入。
因此,非缓冲输出不好,因为它违反了(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的缓冲区包含一个完整的输入行但底层管道还没有准备好进行额外的读取,则它会错误地显示“会阻塞”...我想。)使用
os.fdopen(sys.stdin.fileno(), 'r')
和fcntl
与O_NONBLOCK
进行非阻塞io。(我无法在任何Python版本中让它与readline()配合使用:在Python2.7中,每当出现部分行时,它都会丢失输入;在Python3中,似乎无法区分“将阻塞”和“输入结束”。??)
asyncio(我不确定python2中有哪些内容可用;而且我认为它不能与sys.stdin一起使用;但是,如果仅在从subprocess.Popen()返回的管道中读取时有效,我仍然很感兴趣。)创建一个线程来执行
readline()
循环,并通过queue.Queue将每行传递给主程序;然后,在每次从队列中读取每行之前,主程序可以轮询队列,并在看到它即将阻塞时先刷新stdout。(我尝试过这种方法,并且实际上使其工作了,请参见下面的内容,但是它非常缓慢,比行缓冲要慢得多。)
线程实现:
请注意,这并不严格回答“如何判断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”相比速度要相当。
stdin.readline()
是否会阻塞。因此,我不知道你所期望的实现是否可行。 - g.d.d.c