【发布时间】:2016-12-21 09:06:18
【问题描述】:
我有一个 Python 函数(用 C++ 实现),它从文件描述符(在 C++ 端包装在 FILE* 中)读取,我需要从 asyncio.StreamReader 提供函数。具体来说,reader 就是一个 HTTP 响应的内容:aiohttp.ClientResponse.content。
我想我可能会 open a pipe,将读取端传递给 C++ 函数,并将 connect the write-end 传递给 asyncio 的事件循环。但是,如何通过适当的流控制和尽可能少的复制将数据从流读取器移动到管道?
缺少部分的代码骨架如下:
# obtain the StreamReader from aiohttp
content = aiohttp_client_response.content
# create a pipe
(pipe_read_fd, pipe_write_fd) = os.pipe()
# now I need a suitable protocol to manage the pipe transport
protocol = ?
(pipe_transport, __) = loop.connect_write_pipe(lambda: protocol, pipe_write_fd)
# the protocol should start reading from `content` and writing into the pipe
return pipe_read_fd
【问题讨论】:
标签: python python-asyncio aiohttp