我有一个 Python 函数(用 C++ 实现),它从文件描述符(包装在FILE*
C++ 端)读取,我需要从asyncio.StreamReader
. 具体来说,阅读器是 HTTP 响应的内容:aiohttp.ClientResponse.content。
我想我可能会打开一个管道,将读取端传递给 C++ 函数并将写入端连接到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