summaryrefslogtreecommitdiff
path: root/examples/subprocess_stream.py
blob: 18c64f796c095b312831d6238c23bd7d40c1a547 (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
import asyncio

@asyncio.coroutine
def cat(loop):
    transport, proc = yield from loop.subprocess_shell(asyncio.SubprocessStreamProtocol, "cat")
    print("pid: %s" % transport.get_pid())

    message = "Hello World!"
    print("cat write: %r" % message)
    proc.stdin.write(message.encode('ascii'))
    yield from proc.stdin.drain()

    proc.stdin.close()
    read = yield from proc.stdout.read()
    print("cat read: %r" % read.decode('ascii'))

    returncode = yield from proc.wait()
    print("exit code: %s" % returncode)
    transport.close()

@asyncio.coroutine
def ls(loop):
    transport, proc = yield from loop.subprocess_exec(asyncio.SubprocessStreamProtocol, "ls", stdin=None)
    while True:
        line = yield from proc.stdout.readline()
        if not line:
            break
        print("ls>>", line.decode('ascii').rstrip())
    transport.close()

loop = asyncio.get_event_loop()
loop.run_until_complete(cat(loop))
loop.run_until_complete(ls(loop))