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 1
1
Asyncio documentation
2
2
=====================
3
3
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
6
6
* AsyncIO documentation is written with `Sphinx <http://www.sphinx-doc.org/ >`_.
7
7
8
8
@@ -50,4 +50,4 @@ See also
50
50
51
51
* https://github.com/python/asyncio
52
52
* 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 104
104
# The name of the Pygments (syntax highlighting) style to use.
105
105
pygments_style = 'sphinx'
106
106
107
+ # The default language to highlight source code in.
108
+ highlight_language = 'python3'
109
+
107
110
# A list of ignored prefixes for module index sorting.
108
111
# modindex_common_prefix = []
109
112
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:
7
7
.. literalinclude :: examples/http_client.py
8
8
9
9
For 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
24
24
25
25
tcp_echo.rst
26
26
threads.rst
27
+ subprocess.rst
27
28
producer_consumer.rst
28
29
debug_mode.rst
29
30
@@ -66,4 +67,3 @@ Contributing
66
67
:maxdepth: 2
67
68
68
69
README.rst
69
-
Original file line number Diff line number Diff line change @@ -6,7 +6,7 @@ Simple example
6
6
==============
7
7
8
8
A 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> `_:
10
10
11
11
.. literalinclude :: examples/producer_consumer.py
12
12
@@ -16,11 +16,11 @@ Using task_done()
16
16
17
17
18
18
A 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> `_
20
20
and `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> `_:
22
22
23
- .. literalinclude :: examples/producer_consumer_2 .py
23
+ .. literalinclude :: examples/producer_consumer_join .py
24
24
25
25
For 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::
362
362
$ pip install aiohttp
363
363
364
364
365
- .. _aiohttp : http ://aiohttp.readthedocs.io/en/stable/
365
+ .. _aiohttp : https ://aiohttp.readthedocs.io/en/stable/
366
366
367
367
The whole program looks like this:
368
368
You can’t perform that action at this time.
0 commit comments