GeezerVisonThe World Wide Web, restored to 800 pixels of glory. |
| Home|About|Gaming cabinet|Archives|How it works|Starter sites |
GeezerVison is showing stackabuse.com — Python async/await Tutorial HomeArticles Python async/await TutorialAsynchronous programming has been gaining a lot of traction in the past few years, and for good reason. Although it can be more difficult than the traditional linear style, it is also much more efficient. For example, instead of waiting for an HTTP request to finish before continuing execution, with Python async coroutines you can submit the request and do other work that's waiting in a queue while waiting for the HTTP request to finish. It might take a bit more thinking to get the logic right, but you'll be able to handle a lot more work with less resources. Even then, the syntax and execution of asynchronous functions in languages like Python actually aren't that hard. Now, JavaScript is a different story, but Python seems to execute it fairly well. Asynchronicity seems to be a big reason why Node.js so popular for server-side programming. Much of the code we write, especially in heavy IO applications like websites, depends on external resources. This could be anything from a remote database call to POSTing to a REST service. As soon as you ask for any of these resources, your code is waiting around with nothing to do. With asynchronous programming, you allow your code to handle other tasks while waiting for these other resources to respond. CoroutinesAn asynchronous function in Python is typically called a 'coroutine', which is just a function that uses the
These are special functions that return coroutine objects when called. If you're familiar with JavaScript Promises, then you can think of this returned object almost like a Promise. Calling either of these doesn't actually run them, but instead a coroutine object is returned, which can then be passed to the event loop to be executed later on. In case you ever need to determine if a function is a coroutine or not, Yield fromThere are a few ways to actually call a coroutine, one of which is the The
As you can see,
In order to use this syntax, it must be within another function (typically with the coroutine decorator). Async/awaitThe newer and cleaner syntax is to use the
To actually call this function, we use
Again, just like In Python 3.5, both ways of calling coroutines are supported, but the Running the event loopNone of the coroutine stuff I described above will matter (or work) if you don't know how to start and run an event loop. The event loop is the central point of execution for asynchronous functions, so when you want to actually execute the coroutine, this is what you'll use. The event loop provides quite a few features to you:
Free eBook: Git Essentials Check out our hands-on, practical guide to learning Git, with best-practices, industry-accepted standards, and included cheat sheet. Stop Googling Git commands and actually learn it! While there are actually quite a few configurations and event loop types you can use, most of the programs you write will just need to use something like this to schedule a function:
The last three lines are what we're interested in here. It starts by getting the default event loop ( The Now, you might think this isn't very useful since we end up blocking on the event loop anyway (instead of just the IO calls), but imagine wrapping your entire program in an async function, which would then allow you to run many asynchronous requests at the same time, like on a web server. You could even break off the event loop in to its own thread, letting it handle all of the long IO requests while the main thread handles the program logic or UI. An exampleOkay, so let's see a slightly bigger example that we can actually run. The following code is a pretty simple asynchronous program that fetches JSON from Reddit, parses the JSON, and prints out the top posts of the day from /r/python, /r/programming, and /r/compsci. The first method shown,
This is a bit different than the sample code we showed earlier. In order to get multiple coroutines running on the event loop, we're using To run this, you'll need to install
Now just make sure you run it with Python 3.5 or higher, and you should get an output like this:
Notice that if you run this a few times, the order in which the subreddit data is printed out changes. This is because each of the calls we make releases (yields) control of the thread, allowing another HTTP call to process. Whichever one returns first gets printed out first. ConclusionAlthough Python's built-in asynchronous functionality isn't quite as smooth as JavaScript's, that doesn't mean you can't use it for interesting and efficient applications. Just take 30 minutes to learn its ins and outs and you'll have a much better sense as to how you can integrate this in to your own applications. What do you think of Python's async/await? How have you used it in the past? Let us know in the comments! Follow to keep up with AI and the future of software Practical takes on AI, tooling, and where development is headed. You might also like... |
| You are visitor 01402503 — GeezerVison does not store the pages you look up. Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required. |