Wenn mit der alten Schule subprocess.Popen()
API zwei Prozesse gestartet wird, kann ich leicht anschließen Standard aus von einem Prozess zur Standard in eines anderen Prozess, eine Pipeline zu schaffen, in die gleiche Weise wie ein UNIX-Shell tun wird, wenn Befehle mit |
Verbindungs:Connect zwei Prozesse gestartet mit asyncio.subprocess.create_subprocess_exec()
from subprocess import Popen, PIPE
process_1 = Popen(['ls'], stdout = PIPE)
process_2 = Popen(['wc'], stdin = process_1.stdout)
process_1.wait()
process_2.wait()
wie kann ich das gleiche zu erreichen, wenn den asynchronen API von asyncio.subprocess.create_subprocess_exec()
(oder ähnlichem) unter Verwendung von? Das ist, was ich versucht:
from asyncio.events import get_event_loop
from asyncio.subprocess import PIPE, create_subprocess_exec
async def main():
process_1 = await create_subprocess_exec('ls', stdout = PIPE)
process_2 = await create_subprocess_exec('wc', stdin = process_1.stdout)
await process_1.wait()
await process_2.wait()
get_event_loop().run_until_complete(main())
Aber der zweite Aufruf von create_subprocess_exec()
beschwert sich, dass die zu stdin
gebene Argument hat keine fileno
(was stimmt):
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'
Wie kann ich das gleiche Ergebnis wie im synchronen Beispiel oben?
ohne Bezug: Sie sollten 'process_1.stdout.close()' nach 'Popen (['wc'], ..)' hinzufügen. Siehe [Wie verwende ich subprocess.Popen, um mehrere Prozesse über Pipes zu verbinden?] (Http://stackoverflow.com/q/295459/4279) – jfs