@@ -19,28 +19,40 @@ A Mock Web Server
1919
2020This is a very simple web server. (See below for the code.)
2121Its only purpose is to wait for a given amount of time.
22- Test it by running it from the command line::
22+ Test it by running it from the command line:
23+
24+ .. sourcecode :: console
2325
2426 $ python simple_server.py
2527
26- It will answer like this::
28+ It will answer like this:
29+
30+ .. sourcecode :: console
2731
2832 Serving from port 8000 ...
2933
30- Now, open a browser and go to this URL::
34+ Now, open a browser and go to this URL:
35+
36+ .. sourcecode :: console
3137
3238 http://localhost:8000/
3339
34- You should see this text in your browser::
40+ You should see this text in your browser:
41+
42+ .. sourcecode :: console
3543
3644 Waited for 0.00 seconds.
3745
38- Now, add ``2.5 `` to the URL::
46+ Now, add ``2.5 `` to the URL:
47+
48+ .. sourcecode :: console
3949
4050 http://localhost:8000/2.5
4151
4252After pressing enter, it will take 2.5 seconds until you see this
43- response::
53+ response:
54+
55+ .. sourcecode :: console
4456
4557 Waited for 2.50 seconds.
4658
@@ -49,7 +61,6 @@ Use different numbers and see how long it takes until the server responds.
4961The full implementation looks like this:
5062
5163.. literalinclude :: examples/simple_server.py
52- :language: python
5364
5465Let's have a look into the details.
5566This provides a simple multi-threaded web server:
@@ -113,11 +124,15 @@ The function ``time.perf_counter()`` provides a time stamp.
113124Taking two time stamps a different points in time and calculating their
114125difference provides the elapsed run time.
115126
116- Finally, we can run our client::
127+ Finally, we can run our client:
128+
129+ .. sourcecode :: console
117130
118131 $ python synchronous_client.py
119132
120- and get this output::
133+ and get this output:
134+
135+ .. sourcecode :: console
121136
122137 It took 11.08 seconds for a total waiting time of 11.00.
123138 Waited for 1.00 seconds.
@@ -174,9 +189,7 @@ Therefore, we need to convert our strings in to bytestrings.
174189Next, we read header and message from the reader, which is a ``StreamReader ``
175190instance.
176191We need to iterate over the reader by using a special or loop for
177- ``asyncio ``:
178-
179- .. code-block :: python
192+ ``asyncio ``::
180193
181194 async for raw_line in reader:
182195
@@ -204,14 +217,15 @@ The interesting things happen in a few lines in ``get_multiple_pages()``
204217(the rest of this function just measures the run time and displays it):
205218
206219.. literalinclude :: examples/async_client_blocking.py
207- :language: python
208220 :start-after: pages = []
209221 :end-before: duration
210222
211223We await ``get_page() `` for each page in a loop.
212224This means, we wait until each pages has been retrieved before asking for
213225the next.
214- Let's run it from the command-line to see what happens::
226+ Let's run it from the command-line to see what happens:
227+
228+ .. sourcecode :: console
215229
216230 $ async_client_blocking.py
217231 It took 11.06 seconds for a total waiting time of 11.00.
@@ -249,28 +263,26 @@ The interesting part is in this loop:
249263We append all return values of ``get_page() `` to our lits of tasks.
250264This allows us to send out all request, in our case four, without
251265waiting for the answers.
252- After sending all of them, we wait for the answers, using:
266+ After sending all of them, we wait for the answers, using::
253267
254268 await asyncio.gather(*tasks)
255269
256270The difference here is the use of ``asyncio.gather() `` that is called with all
257271our tasks in the list ``tasks `` as arguments.
258- The ``asyncio.gather(*tasks) `` means for our example with four list entries:
259-
260- .. code-block :: python
272+ The ``asyncio.gather(*tasks) `` means for our example with four list entries::
261273
262274 asyncio.gather(tasks[0], tasks[1], tasks[2], tasks[3])
263275
264- So, for a list with 100 tasks it would mean:
265-
266- .. code-block :: python
276+ So, for a list with 100 tasks it would mean::
267277
268278 asyncio.gather(tasks[0], tasks[1], tasks[2],
269279 # 96 more tasks here
270280 tasks[99])
271281
272282
273- Let's see if we got any faster::
283+ Let's see if we got any faster:
284+
285+ .. sourcecode :: console
274286
275287 $ async_client_nonblocking.py
276288 It took 5.08 seconds for a total waiting time of 11.00.
@@ -309,10 +321,11 @@ High-Level Approach with ``aiohttp``
309321
310322The library aiohttp _ allows to write HTTP client and server applications,
311323using a high-level approach.
312- Install with::
324+ Install with:
313325
314- $ pip install aiohttp
326+ .. sourcecode :: console
315327
328+ $ pip install aiohttp
316329
317330.. _aiohttp : https://aiohttp.readthedocs.io/en/stable/
318331
@@ -352,7 +365,9 @@ with ``asyncio``.
352365The only difference is the opened client session and handing over this session
353366to ``fetch_page() `` as the first argument.
354367
355- Finally, we run this program::
368+ Finally, we run this program:
369+
370+ .. sourcecode :: console
356371
357372 $ python aiohttp_client.py
358373 It took 5.04 seconds for a total waiting time of 11.00.
0 commit comments