[0:02] when chatting with someone on Jupiter's [0:03] thirdd largest moon you will notice a [0:06] distinct lag in Communications that is [0:08] due to async [0:14] IO when you try to perform a task that [0:16] requires a lot of waiting you may notice [0:19] an annoying lag in execution you can [0:21] solve this with async IO this module [0:25] brings the await and async syntax to [0:27] python this pair of felet words empowers [0:30] you with the tools to easily manage a [0:33] collection of laggy [0:34] tasks sometimes impatience is a [0:40] virtue code can be categorized as [0:43] synchronous or [0:44] asynchronous in synchronous programming [0:47] tasks are performed one at a time in the [0:49] order they are called each task must [0:52] finish before the next one can [0:54] begin with asynchronous programming [0:56] however different tasks can start [0:59] process and finish in overlapping [1:01] periods of time for example imagine a [1:04] task that submits many HTTP requests in [1:08] the synchronous World these HTTP [1:11] requests must be completed one at a time [1:14] in the asynchronous world each request [1:17] can start and be set aside while waiting [1:19] for a response during this waiting [1:21] period different tasks can step forward [1:24] and use the Computing resources this [1:27] nonlinear approach saves one of the most [1:29] Val resources around time to better [1:34] understand the mechanics of synchronous [1:35] and asynchronous execution you need to [1:37] learn about sub routines and co-routines [1:40] a sub routine is a block of code that [1:42] can be called as needed when a sub [1:44] routine is called control of the program [1:47] is transferred to that sub routine when [1:49] its work is done control is returned [1:52] back to the main program and execution [1:54] continues from where it left off sub [1:57] routines cannot be paused and resumed [2:00] they run until [2:02] done a co- routine is a special kind of [2:05] function that can have its executions [2:07] paused and resumed this is possible [2:10] because they maintain their state [2:12] between pauses co- routines are perfect [2:15] for tasks that need to wait for [2:17] something like IO operations database [2:20] calls and HTTP requests the terms sub [2:24] routine and co-routine are derived from [2:27] the roles they play and how they [2:28] function sub routine is a delightful [2:31] blend of sub and routine as the name [2:35] suggests it is a subset of a larger [2:37] program co- routine is a ju toos of Co [2:41] and routine Co for together and routine [2:46] for [2:47] routine this name was chosen by someone [2:49] to describe routines that can run [2:52] cooperatively co-routines are the key [2:54] ingredient to make asynchronous [2:56] programming or concurrency possible in [2:59] in a typical single-threaded app all of [3:02] the code and sub routines run [3:04] sequentially this is simple but can be [3:07] wasteful there are two different [3:08] approaches that make better use of [3:10] available Computing resources [3:12] concurrency and [3:14] parallelism using concurrency the start [3:17] and stop times of multiple co- routines [3:20] can overlap with parallelism different [3:23] threads can execute at the same time is [3:27] there going to be any python in this [3:28] python lesson [3:30] yes yes there is but before we can enter [3:34] python mode I must import an important [3:37] message socratica is now in the course [3:39] business visit socratic.com to see the [3:42] excitement for yourself and for an [3:45] asynchronous update about future courses [3:47] feverishly type your email address in a [3:50] text box somewhere on the site now back [3:54] to python we are now going to show you [3:56] how to use concurrency in Python with [3:58] the async and await keywords and the [4:01] async io [4:03] module let us create two functions brew [4:08] coffee and toast [4:12] Bagel The Brew Coffee function will use [4:14] the sleep function to simulate a command [4:17] that takes 3 seconds to complete the [4:19] toast Bagel function will pretend it [4:21] takes 2 seconds to complete with this [4:24] 5-second toasty breakfast the future is [4:26] fast and [4:28] carbolicious we will record a start time [4:31] call both [4:33] functions record a stop [4:35] time compute the elaps time and print [4:38] the [4:44] results [4:50] run this code took 5 seconds and a bit [4:53] more to execute but there is no reason [4:56] to wait for the coffee to finish before [4:58] you start toasting the B Bagel such [5:00] inefficiency gives the secretly embedded [5:03] AI systems far too much time for [5:05] Mischief let us modify this code so it [5:08] runs [5:09] concurrently with [5:11] co-routines to turn each function into a [5:14] co- routine add the async keyword this [5:18] is the first step but not the last as is [5:21] evident if you run using the async [5:24] keyword to make a co- routine is [5:26] necessary but not sufficient you also [5:29] need to to specify where in the co- [5:30] routine it is safe to pause and yield [5:33] control to other co- routines you do [5:35] this with the await [5:38] keyword this brings us to another [5:40] essential concept you can only put await [5:43] in front of commands that are awaitable [5:46] the sleep function is not [5:48] awaitable luckily the async io module [5:51] gives you an awaitable version of the [5:53] sleep [5:54] function you can call the co- routines [5:57] individually or in a batch we will first [6:00] demonstrate the batch approach the async [6:03] io module has a gather function to group [6:05] co- routines for concurrent execution [6:08] the arguments determine which co- [6:10] routines will run [6:11] concurrently the arguments may appear to [6:13] be regular function calls but they are [6:15] not calling a co-routine returns a [6:18] co-routine object not a typical return [6:21] value these co-routine objects give [6:24] async IO the ability to start and stop [6:27] their [6:28] execution to get the return values from [6:30] the co- routines you need to await them [6:34] and note that you await the return [6:35] values in the same order you pass the [6:37] co- routines to The Gather function [6:40] order matters and the use of a weight [6:43] here is not optional after all we have [6:47] to wait for both co- routines to finish [6:50] so it only makes sense that we have to [6:51] wait on the batch too we are so close to [6:54] the finish line but there is one final [6:57] step see how we await for the batch [7:00] results any function that has an await [7:02] keyword must be declared async this [7:06] means we must put the async keyword [7:08] before the main [7:09] function because main has now become a [7:12] co-routine we have to call it in a [7:14] slightly different way the async io [7:17] module has you covered just call the Run [7:20] function and pass in the main co- [7:22] routine now inhale widen your eyes and [7:28] run [7:32] Joy of Joys our breakfast was completed [7:35] in 3 seconds instead of five time saved [7:39] me [7:41] happy there is an alternative to using [7:43] the gather function you can use the [7:46] create task function in the async io [7:48] module to create a task out of each co- [7:52] routine then you can await each co- [7:54] routine [7:57] individually run [8:03] just as fast just as happy so which is [8:08] better working in batches or with [8:10] individual tasks it is your choice so [8:14] Choose Wisely computer chips want to [8:17] compute that is their singular purpose [8:19] in life the async io module was built to [8:23] help chips realize their full potential [8:26] by adding a pause button to objects you [8:28] enable them to take turns sharing is [8:31] caring after all and speaking of sharing [8:34] many human people ask you to share their [8:36] videos with as many human people as [8:38] possible this is not an efficient use of [8:41] resources instead await the opportunity [8:44] to yield this video to someone who has [8:47] finished a task and is thinking about [8:50] the [8:54] [Music] [8:58] future [9:02] [Music]