This repository was archived by the owner on May 31, 2021. It is now read-only.
File tree Expand file tree Collapse file tree 10 files changed +92
-11
lines changed
Expand file tree Collapse file tree 10 files changed +92
-11
lines changed Original file line number Diff line number Diff line change 11Asyncio documentation
22=====================
33
4- * Online doc: http ://asyncio.readthedocs.io/
5- * GitHub: https://github.com/haypo /asyncio-doc
4+ * Online doc: https ://asyncio.readthedocs.io/
5+ * GitHub: https://github.com/asyncio-doc /asyncio-doc
66* AsyncIO documentation is written with `Sphinx <http://www.sphinx-doc.org/ >`_.
77
88
@@ -50,4 +50,4 @@ See also
5050
5151* https://github.com/python/asyncio
5252* http://krondo.com/an-introduction-to-asynchronous-programming-and-twisted/
53- * http ://curio.readthedocs.io/en/latest/tutorial.html
53+ * https ://curio.readthedocs.io/en/latest/tutorial.html
Original file line number Diff line number Diff line change 104104# The name of the Pygments (syntax highlighting) style to use.
105105pygments_style = 'sphinx'
106106
107+ # The default language to highlight source code in.
108+ highlight_language = 'python3'
109+
107110# A list of ignored prefixes for module index sorting.
108111# modindex_common_prefix = []
109112
File renamed without changes.
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 @@ -7,4 +7,4 @@ HTTP client example:
77.. literalinclude :: examples/http_client.py
88
99For more information, see `aiohttp documentation
10- <http ://aiohttp.readthedocs.io/> `_.
10+ <https ://aiohttp.readthedocs.io/> `_.
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 @@ -6,7 +6,7 @@ Simple example
66==============
77
88A simple producer/consumer example, using an `asyncio.Queue
9- <http ://docs.python.org/3/library/asyncio-queue.html#asyncio.Queue> `_:
9+ <https ://docs.python.org/3/library/asyncio-queue.html#asyncio.Queue> `_:
1010
1111.. literalinclude :: examples/producer_consumer.py
1212
@@ -16,11 +16,11 @@ Using task_done()
1616
1717
1818A simple producer/consumer example, using `Queue.task_done
19- <http ://docs.python.org/3/library/asyncio-queue.html#asyncio.Queue.task_done> `_
19+ <https ://docs.python.org/3/library/asyncio-queue.html#asyncio.Queue.task_done> `_
2020and `Queue.join
21- <http ://docs.python.org/3/library/asyncio-queue.html#asyncio.Queue.join> `_:
21+ <https ://docs.python.org/3/library/asyncio-queue.html#asyncio.Queue.join> `_:
2222
23- .. literalinclude :: examples/producer_consumer_2 .py
23+ .. literalinclude :: examples/producer_consumer_join .py
2424
2525For more information, see the `asyncio queue documentation
26- <http ://docs.python.org/3/library/asyncio-queue.html> `_.
26+ <https ://docs.python.org/3/library/asyncio-queue.html> `_.
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> `_.
Original file line number Diff line number Diff line change @@ -362,7 +362,7 @@ Install with::
362362 $ pip install aiohttp
363363
364364
365- .. _aiohttp : http ://aiohttp.readthedocs.io/en/stable/
365+ .. _aiohttp : https ://aiohttp.readthedocs.io/en/stable/
366366
367367The whole program looks like this:
368368
You can’t perform that action at this time.
0 commit comments