blob: d8a62420f29a846a47794378988883e0a893db5c (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
|
#!/usr/bin/env python3
"""Example showing how to attach a read pipe to a subprocess."""
import asyncio
import os, sys
code = """
import os, sys
fd = int(sys.argv[1])
os.write(fd, b'data')
os.close(fd)
"""
loop = asyncio.get_event_loop()
@asyncio.coroutine
def task():
rfd, wfd = os.pipe()
args = [sys.executable, '-c', code, str(wfd)]
pipe = open(rfd, 'rb', 0)
reader = asyncio.StreamReader(loop=loop)
protocol = asyncio.StreamReaderProtocol(reader, loop=loop)
transport, _ = yield from loop.connect_read_pipe(lambda: protocol, pipe)
proc = yield from asyncio.create_subprocess_exec(*args, pass_fds={wfd})
yield from proc.wait()
os.close(wfd)
data = yield from reader.read()
print("read = %r" % data.decode())
loop.run_until_complete(task())
loop.close()
|