我正在编写一个脚本,该脚本通过命名管道从另一个软件读取数据。我只想在可用时读取数据,并且我正在尝试使用 add_reader
from asyncio
.
我注意到,在 Linux 上,我注册的阅读器会在管道关闭后继续被调用。在 macOS 上,这不会发生。
这让我百思不得其解,因为在管道的写端挂掉之后,我不会指望读端还能读,尤其是明明不可能没有数据。
这个脚本说明了问题:
#!/usr/bin/env python3
import os, asyncio, threading, time
NAMED_PIPE = 'write.pipe'
# Setup the named pipe
if os.path.exists(NAMED_PIPE):
os.unlink(NAMED_PIPE)
os.mkfifo(NAMED_PIPE)
loop = asyncio.get_event_loop()
def simulate_write():
# Open the pipe for writing and write something into it.
# This simulates another process
print('waiting for opening pipe for writing')
with open(NAMED_PIPE, 'w') as write_stream:
print('writing pipe opened')
time.sleep(1)
print('writing some data')
print('<some data>', file=write_stream)
time.sleep(1)
print('exiting simulated write')
async def open_pipe_for_reading():
print('waiting for opening pipe for reading')
# This needs to run asynchronously because open will
# not reuturn until on the other end, someone tries
# to write
return open(NAMED_PIPE)
count = 0
def read_data_block(fd):
global count
count += 1
print('reading data', fd.read())
if count > 10:
print('reached maximum number of calls')
loop.remove_reader(fd.fileno())
# Spawn a thread that will simulate writing
threading.Thread(target=simulate_write).start()
# Get the result of open_pipe_for_reading
stream = loop.run_until_complete(open_pipe_for_reading())
print('reading pipe opened')
# Schedule the reader
loop.add_reader(stream.fileno(), read_data_block, stream)
try:
loop.run_forever()
except KeyboardInterrupt:
pass
finally:
print('closing stream')
stream.close()
print('removing pipe')
os.unlink(NAMED_PIPE)
在 OSX 上,这是我观察到的行为:
waiting for opening pipe for writing
waiting for opening pipe for reading
reading pipe opened
writing pipe opened
writing some data
exiting simulated write
reading data <some data>
^Cclosing stream
removing pipe
在 Linux 上:
waiting for opening pipe for writing
waiting for opening pipe for reading
reading pipe opened
writing pipe opened
writing some data
exiting simulated write
reading data <some data>
reading data
reading data
reading data
reading data
reading data
reading data
reading data
reading data
reading data
reading data
reached maximum number of calls
^C<closing stream
removing pipe
那么,为什么一个没有数据的封闭管道可以读取呢?
此外,据我了解,add_reader
会在可以从读取流并且有一些数据要读取时触发; 这个解释正确吗?
Python 和操作系统版本:
- Python 3.6.4 (MacPorts)、macOS High Sierra 10.13.3 (17D102)
- Python 3.6.1(手动编译)CentOS Linux release 7.4.1708 (Core)
- Python 3.5.2(来自 repo)Linux Mint 18.2 Sonya
最佳答案
在 python 中读取空数据是套接字/管道关闭的标志。
data = fd.read()
if not data:
return
另外请将管道切换到非阻塞模式:
os.set_blocking(fd, False)
关于Python 和异步 : closed named pipe always available for reading,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49501263/