diff options
Diffstat (limited to 'examples/shell.py')
-rw-r--r-- | examples/shell.py | 27 |
1 files changed, 15 insertions, 12 deletions
diff --git a/examples/shell.py b/examples/shell.py index 7dc7caf..5d6158a 100644 --- a/examples/shell.py +++ b/examples/shell.py @@ -1,31 +1,33 @@ """Examples using create_subprocess_exec() and create_subprocess_shell().""" -import asyncio +import trollius as asyncio +from trollius import From import signal -from asyncio.subprocess import PIPE +from trollius.subprocess import PIPE +from trollius.py33_exceptions import ProcessLookupError @asyncio.coroutine def cat(loop): - proc = yield from asyncio.create_subprocess_shell("cat", + proc = yield From(asyncio.create_subprocess_shell("cat", stdin=PIPE, - stdout=PIPE) + stdout=PIPE)) print("pid: %s" % proc.pid) message = "Hello World!" print("cat write: %r" % message) - stdout, stderr = yield from proc.communicate(message.encode('ascii')) + stdout, stderr = yield From(proc.communicate(message.encode('ascii'))) print("cat read: %r" % stdout.decode('ascii')) - exitcode = yield from proc.wait() + exitcode = yield From(proc.wait()) print("(exit code %s)" % exitcode) @asyncio.coroutine def ls(loop): - proc = yield from asyncio.create_subprocess_exec("ls", - stdout=PIPE) + proc = yield From(asyncio.create_subprocess_exec("ls", + stdout=PIPE)) while True: - line = yield from proc.stdout.readline() + line = yield From(proc.stdout.readline()) if not line: break print("ls>>", line.decode('ascii').rstrip()) @@ -35,10 +37,11 @@ def ls(loop): pass @asyncio.coroutine -def test_call(*args, timeout=None): +def test_call(*args, **kw): + timeout = kw.pop('timeout', None) try: - proc = yield from asyncio.create_subprocess_exec(*args) - exitcode = yield from asyncio.wait_for(proc.wait(), timeout) + proc = yield From(asyncio.create_subprocess_exec(*args)) + exitcode = yield From(asyncio.wait_for(proc.wait(), timeout)) print("%s: exit code %s" % (' '.join(args), exitcode)) except asyncio.TimeoutError: print("timeout! (%.1f sec)" % timeout) |