This repository was archived by the owner on May 31, 2021. It is now read-only.
File tree Expand file tree Collapse file tree 4 files changed +79
-1
lines changed
Expand file tree Collapse file tree 4 files changed +79
-1
lines changed Original file line number Diff line number Diff line change 1+ import asyncio
2+
3+
4+ async def run_command (* args ):
5+ # Create subprocess
6+ process = await asyncio .create_subprocess_exec (
7+ * args ,
8+ # stdout must a pipe to be accessible as process.stdout
9+ stdout = asyncio .subprocess .PIPE )
10+ # Wait for the subprocess to finish
11+ stdout , stderr = await process .communicate ()
12+ # Return stdout
13+ return stdout .decode ().strip ()
14+
15+
16+ loop = asyncio .get_event_loop ()
17+ # Gather uname and date commands
18+ commands = asyncio .gather (run_command ('uname' ), run_command ('date' ))
19+ # Run the commands
20+ uname , date = loop .run_until_complete (commands )
21+ # Print a report
22+ print ('uname: {}, date: {}' .format (uname , date ))
23+ loop .close ()
Original file line number Diff line number Diff line change 1+ import asyncio
2+
3+
4+ async def echo (msg ):
5+ # Run an echo subprocess
6+ process = await asyncio .create_subprocess_exec (
7+ 'cat' ,
8+ # stdin must a pipe to be accessible as process.stdin
9+ stdin = asyncio .subprocess .PIPE ,
10+ # stdout must a pipe to be accessible as process.stdout
11+ stdout = asyncio .subprocess .PIPE )
12+ # Write message
13+ print ('Writing {!r} ...' .format (msg ))
14+ process .stdin .write (msg .encode () + b'\n ' )
15+ # Read reply
16+ data = await process .stdout .readline ()
17+ reply = data .decode ().strip ()
18+ print ('Received {!r}' .format (reply ))
19+ # Stop the subprocess
20+ process .terminate ()
21+ code = await process .wait ()
22+ print ('Terminated with code {}' .format (code ))
23+
24+
25+ loop = asyncio .get_event_loop ()
26+ loop .run_until_complete (echo ('hello!' ))
27+ loop .close ()
Original file line number Diff line number Diff line change @@ -24,6 +24,7 @@ Chapter 2: Advanced topics
2424
2525 tcp_echo.rst
2626 threads.rst
27+ subprocess.rst
2728 producer_consumer.rst
2829 debug_mode.rst
2930
@@ -66,4 +67,3 @@ Contributing
6667 :maxdepth: 2
6768
6869 README.rst
69-
Original file line number Diff line number Diff line change 1+ ++++++++++
2+ Subprocess
3+ ++++++++++
4+
5+ Run a subprocess and read its output
6+ ====================================
7+
8+ A simple example to run commands in a subprocess using
9+ `asyncio.create_subprocess_exec
10+ <https://docs.python.org/3.6/library/asyncio-subprocess.html#asyncio.create_subprocess_exec> `_
11+ and get the output using `process.communicate
12+ <https://docs.python.org/3/library/asyncio-subprocess.html#asyncio.asyncio.subprocess.Process.communicate> `_:
13+
14+ .. literalinclude :: examples/subprocess_command.py
15+
16+
17+ Communicate with a subprocess using standard streams
18+ ====================================================
19+
20+ A simple example to communicate with an echo subprocess using `process.stdin
21+ <https://docs.python.org/3/library/asyncio-subprocess.html#asyncio.asyncio.subprocess.Process.stdin> `_
22+ and `process.stdout
23+ <https://docs.python.org/3/library/asyncio-subprocess.html#asyncio.asyncio.subprocess.Process.stdout> `_:
24+
25+ .. literalinclude :: examples/subprocess_echo.py
26+
27+ For more information, see the `asyncio subprocess documentation
28+ <https://docs.python.org/3/library/asyncio-subprocess.html> `_.
You can’t perform that action at this time.
0 commit comments