But the second call to create_subprocess_exec() complains that the argument passed to stdin has no fileno (which is true):Traceback (most recent call last): File ".../test-async.py", line 11, in <module> get_event_loop().run_until_complete(main())[...] File ".../test-async.py", line 6, in main process_2 = await create_subprocess_exec('wc', stdin = process_1.stdout)[...] File "/opt/local/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/subprocess.py", line 1388, in _get_handles p2cread = stdin.fileno()AttributeError: 'StreamReader' object has no attribute 'fileno'如何获得与上面同步示例相同的结果?How can I get the same result as in the synchronous example above?推荐答案在 asyncio 中,process.stdout 实际上是一个 StreamReader,不是文件对象.可以通过process._transport._proc.stdout 访问文件对象.不幸的是,您将无法使用它,因为它已经在事件循环中注册以提供流接口 process.stdout.In asyncio, process.stdout is actually a StreamReader, not a file object. The file object can be accessed through process._transport._proc.stdout. Unfortunately, you won't be able to use it since it has already been registered in the event loop in order to provide the stream interface process.stdout.处理该问题的一种方法是创建自己的管道并将文件描述符传递给子进程:One way to deal with the issue is to create your own pipe and pass the file descriptors to the subprocess: async def main(): read, write = os.pipe() process_1 = await create_subprocess_exec('ls', stdout=write) os.close(write) process_2 = await create_subprocess_exec('wc', stdin=read, stdout=PIPE) os.close(read) return await process_2.stdout.read()请注意,write 文件描述符应在第一个子进程启动后显式关闭(除非您使用 subprocess.PIPE,否则它不会自动关闭).read 文件描述符也需要关闭,如 此处.Note that the write file descriptor should be explicitly closed once the first subprocess is started (it is not automatically closed unless you use subprocess.PIPE). The read file descriptor also needs to be closed, as explained here. 这篇关于连接两个以 asyncio.subprocess.create_subprocess_exec() 启动的进程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!
11-03 02:36