High Performance Python by Micha Gorelick

High Performance Python by Micha Gorelick

Author:Micha Gorelick [Gorelick, Micha]
Language: eng
Format: epub, pdf
Publisher: O'Reilly Media
Published: 2020-04-29T00:00:00+00:00


As in the gevent example, we must use a semaphore to limit the number of requests.

We return a new coroutine that will asynchronously download files and respect the locking of the semaphore.

The http_client function returns futures. To keep track of progress, we save the futures into a list.

As with gevent, we can wait for futures to become ready and iterate over them.

One immediate reaction to this code is the number of async with, async def, and await calls. In the definition for http_get, we use the async context manager to get access to shared resources in a concurrent-friendly way. That is to say, by using async with, we allow other coroutines to run while waiting to acquire the resources we are requesting. As a result, sharing things such as open semaphore slots or already opened connections to our host can be done more efficiently than we experienced with tornado.

In fact, the call graph in Figure 8-6 shows a smooth transition similar to that of gevent in Figure 8-4. Furthermore, the asyncio code runs slightly faster than the gevent code overall (1.10 seconds versus 1.14 seconds—see Table 8-1), even though the time for each request is slightly longer. This can be explained only by a faster resumption of coroutines paused by the semaphore or waiting for the HTTP client.



Download



Copyright Disclaimer:
This site does not store any files on its server. We only index and link to content provided by other sites. Please contact the content providers to delete copyright contents if any and email us, we'll remove relevant links or contents immediately.