diff options
| author | Victor Stinner <victor.stinner@gmail.com> | 2014-02-01 22:46:32 +0100 |
|---|---|---|
| committer | Victor Stinner <victor.stinner@gmail.com> | 2014-02-01 22:46:32 +0100 |
| commit | ea6b4e215be5da305bde53aa84fd11148ec3d1b0 (patch) | |
| tree | 582d27c481add22647a870cc0bf619619d3d64d3 /examples/subprocess_attach_write_pipe.py | |
| parent | 6dd8d720a3399f8ab9079571a55d5b706ab073f3 (diff) | |
| download | trollius-ea6b4e215be5da305bde53aa84fd11148ec3d1b0.tar.gz | |
Merge (manually) the subprocess_stream into default
* Add a new asyncio.subprocess module
* Add new create_subprocess_exec() and create_subprocess_shell() functions
* The new asyncio.subprocess.SubprocessStreamProtocol creates stream readers
for stdout and stderr and a stream writer for stdin.
* The new asyncio.subprocess.Process class offers an API close to the
subprocess.Popen class:
- pid, returncode, stdin, stdout and stderr attributes
- communicate(), wait(), send_signal(), terminate() and kill() methods
* Remove STDIN (0), STDOUT (1) and STDERR (2) constants from base_subprocess
and unix_events, to not be confused with the symbols with the same name of
subprocess and asyncio.subprocess modules
* _ProactorBasePipeTransport.get_write_buffer_size() now counts also the size
of the pending write
* _ProactorBaseWritePipeTransport._loop_writing() may now pause the protocol if
the write buffer size is greater than the high water mark (64 KB by default)
* Add new subprocess examples: shell.py, subprocess_shell.py,
* subprocess_attach_read_pipe.py and subprocess_attach_write_pipe.py
Diffstat (limited to 'examples/subprocess_attach_write_pipe.py')
| -rw-r--r-- | examples/subprocess_attach_write_pipe.py | 33 |
1 files changed, 33 insertions, 0 deletions
diff --git a/examples/subprocess_attach_write_pipe.py b/examples/subprocess_attach_write_pipe.py new file mode 100644 index 0000000..017b827 --- /dev/null +++ b/examples/subprocess_attach_write_pipe.py @@ -0,0 +1,33 @@ +#!/usr/bin/env python3 +"""Example showing how to attach a write pipe to a subprocess.""" +import asyncio +import os, sys +from asyncio import subprocess + +code = """ +import os, sys +fd = int(sys.argv[1]) +data = os.read(fd, 1024) +sys.stdout.buffer.write(data) +""" + +loop = asyncio.get_event_loop() + +@asyncio.coroutine +def task(): + rfd, wfd = os.pipe() + args = [sys.executable, '-c', code, str(rfd)] + proc = yield from asyncio.create_subprocess_exec( + *args, + pass_fds={rfd}, + stdout=subprocess.PIPE) + + pipe = open(wfd, 'wb', 0) + transport, _ = yield from loop.connect_write_pipe(asyncio.Protocol, + pipe) + transport.write(b'data') + + stdout, stderr = yield from proc.communicate() + print("stdout = %r" % stdout.decode()) + +loop.run_until_complete(task()) |
