Subscribe to RSS
The Mouse Vs. The Python
The asyncio module was added to Python in version 3. What that means is that it is possible that asyncio receives backwards incompatible changes or could even be removed in a future release of Python. This chapter is not meant to cover everything you can do with asyncio, however you will learn how to use the module and why it is useful. If you need something like asyncio in an older version of Python, then you might want to take a look at Twisted or gevent. The asyncio module provides a framework that revolves around the event loop. An event loop basically waits for something to happen and then acts on the event. Asyncio actually has several loop implementations available to it. The module will default to the one most likely to be the most efficient for the operating system it is running under; however you can explicitly choose the event loop if you so desire. Think of a server as it waits for someone to come along and ask for a resource, such as a web page. But when it does get a hit, then the server needs to react. This reaction is known as event handling. When a user loads the web page, the server will check for and call one or more event handlers. Once those event handlers are done, they need to give control back to the event loop. To do this in Python, asyncio uses coroutines. A coroutine is a special function that can give up control to its caller without losing its state. A coroutine is a consumer and an extension of a generator. Instead it will return a coroutine object that you can pass to the event loop to have it executed either immediately or later on. One other term you will likely run across when you are using the asyncio module is future. Your event loop can watch future objects and wait for them to finish. When a future finishes, it is set to done. Asyncio also supports locks and semaphores. The last piece of information I want to mention is the Task. A Task is a wrapper for a coroutine and a subclass of Future. You can even schedule a Task using the event loop. The async and await keywords were added in Python 3. This decorator still works in Python 3. Starting in Python 3. So the function above would end up looking like this:. When you define a coroutine in this manner, you cannot use yield inside the coroutine function. Instead it must include a return or await statement that are used for returning values to the caller. Note that the await keyword can only be used inside an async def function. While it is certainly helpful to have a lot of background information into how all this works, sometimes you just want to see some examples so you can get a feel for the syntax and how to put things together. A fairly common task that you will want to complete is downloading a file from some location whether that be an internal resource or a file on the Internet. Usually you will want to download more than one file. In this code, we import the modules that we need and then create our first coroutine using the async syntax. When it is done, it will return a message that says so. The other coroutine is our main coroutine. It basically takes a list of one or more URLs and queues them up. Of course, to actually start the coroutines, they need to be added to the event loop.