 So next up, we have a talk by Korean about demystifying Async and await keywords and Python and JavaScript. Over to you, Korean. Thank you, Bhavin. Am I audible? Yep, loud and clear. I hope yes. So today I will be talking about Async and Avi, which is a way to write concurrent code in both languages like Python, JavaScript, and a few more languages. It's a foundation for multiple asynchronous frameworks that provide high performance and high performance for network and web servers. So during this talk, we will be covering what is the fundamental concept behind Async and await. Also, we will be looking about what is a thing of it and how we can write concurrent code in the context of both Python and JavaScript. Also, we will be looking what are the applications and what are some of the potential mistakes which you can make as a beginner when you are writing Async and await code. So a bit about myself. Myself as Bhavin, I'm Korean and I'm working as a software engineer cum data scientist at AOT Technologies, building forms for AI and open source solution. I'm an open source enthusiast and Python is the language which got me into programming. So if you are interested to follow along with the slides, you can check the link bit.ly-async-avai-pycon. So the first time I used the keywords Async and await, I was confused. I asked the people near me, what does this keyword Async and await, which is so on Pythonic doing? And they told me it was used to improve the speed of the program. Hmm, interesting. I thought and I started looking more and more about it. Once I started looking more and more, I got confused and I got into jargons like event loads, tasks, promises, callbacks and a lot more. If you are attending this talk, I am assuming you are also confused by asynchronous programming in both Python and JavaScript. So with this talk, we will be dignifying it one by one. So let's understand the fundamental concept of Async and await with each example. So I hope you know the person in the picture. This is Vishwanathan Anand, the five-time world champion and still India's best player. Assume that we are organizing a chess simul with 24 opponents and each of the opponent is rated with less than 1500 yellow point, which is a chess rating system. So usually 1500 yellow point players are having one years of experience and they are relatively new to the game. And assume that each game is ending in 30 moves and since Anand is an experienced player, he is moving in five seconds while the opponent, since he or she may be a low rated player, will take 55 seconds to make him move. So assume that if Anand was playing the game, one player like it at a time in the simul, just like a normal chess game. So the one game will take approximately 30 minutes and almost 24 games will take 24 to 30 minutes that is equal to 12 hours to complete. Usually chess simul are not run that way. In a real chess simul, it is usually asynchronous. So what usually happens is like Anand will make the first move, then he will move on to the second vote and to the corresponding votes. So as a result, Anand is able to play all the 24 opponents in two minutes the first time. After that, by that time the opponent will be, the first opponent will be ready for the move. So by this method, we are able to complete the entire set of game in one hour because if we are playing 30 moves, 30 into 2 minutes per each round, when we are playing 24 opponents, we will be able to complete in one hour. So if you look at this compared to our asynchronous chess simul example, we are almost 12x faster with this approach. So if you are looking at the bottleneck in the chess simul example, you will see that our bottleneck was to utilize Anand to his fullest potential. In the synchronous chess simul example, we were not utilizing him fully, but in the asynchronous chess simul example, since he is playing one player at a time, not playing from one player, he is moving on to the other player, he is playing very quickly. So such a problem in computer science is called input-output bond problem. It refers to the time, if more time is spent for waiting rather than actually processing it, such a problems are called input-output bond problems. So we usually have a solution for this that is used by using concurrency. So using concurrency, we are able to run different parts of the program or algorithm to be executed in the same time or out of order. So this is like a person juggling in this picture. Now assume that if we are organizing a different kind of chess simul. This time, we are not having the normal players, but we are having grandmasters who are almost of the same level like Vishwanathan Anand. They have been playing the game for years. So Vishwanathan Anand is not able to play as quickly as he used to be playing with the low rated opponents. So what can we do? A hypothetical situation which can be shown in this picture will be to add more and more Anand. And if more and more Anand are there, we can obviously play the game faster. I know this is not possible in the real world, but for computers, most of the computers are having multiple cores. So with octa cores and hexa cores, we are able to run our program in all the cores and we are able to run multiple tasks at the time. So what is the difference between concurrency and parallelism? So concurrency can be defined as a way to run multiple tasks with an ability to run it in overlapping manner. While parallelism is when tasks are executed at the same instant. So one is about the structure and the other is about the execution. If you are interested to learn more, I will highly recommend you to check out the first API talks on Asynchronousity. And you may be thinking which is better, whether we should use concurrency for all our problems or parallelism for all our problems. It depends on the task which you are doing. So that documentation will give you a good idea about it. So what is the keywords async and await? So I will like to define it from a layman's perspective for our sake of discussion as async is a way to write concurrent code. And await can be used to train all the code can't be running concurrently, right? Some code need to wait so that such code to have a waiting condition we are using the await keyword. So you may have been guessing why I am talking async and await in the context of both Python and JavaScript. So according to 2020 JetBrains survey, if you look at the Python usage with other languages, then JavaScript is the most commonly used language with Python among the developer community. So that's why I am talking about the Python usage with JavaScript in the stock. So now let's look at the look at a synchronous program and let's see how we can make it faster with an asynchronous programming in Python. So you can see this piece of code snippet. So you can see that we have a great after function. What the great after function is doing is it is having it. It is we are passing a delay and it is sleeping for one second. After that we are greeting the corresponding person with hello, person name and with a waving hand emoji. So you can see that in our main function we have called the great after function. So we are greeting Guido and Sebastian. So if you are looking at the output, you can see that how the output is coming as expected. So first we are greeting Guido and Rosam with three seconds delay followed by Sebastian Ramirez with two seconds delay and Luciano Ramirez with one second delay. And the program is taking six seconds to execute. So if you want to visualize how it is really happening, we had three functions and function one took a long time and function two also function two had two seconds delay. So since the program was running synchronously, you can see that response two, even though it was having a small delay, it had to wait for a long time. And even response three also had to wait for a long time before it was being executed. So you may be thinking how we can improve the performance like in case of the chess simul example with Anand when we played synchronously, we were able to get a 12x improvement. So there are mainly two ways to implement concurrency in CPython. It is using threading library and using the Asingger module. So if you look at the threading module, Python threads are implemented internally using the operating system threads. And in CPython, due to the global interpreter log, only one thread can be executed so that Python codeband can be run only once. So it's highly suitable for, it's threading library is suitable for input output concurrency. So let's look at a sample code to see how we can do the same code concurrently. So if you see that we are importing the threading module and after that we are using the threading.thread module and we are passing the target and finally you are starting all the thread with thread.start module, thread.start function. So I guess you may have estimated this output. So first we are greeting Luciano Ramaldo. Even though it was coming at the third position, since Luciano Ramaldo has the least time delay it is being printed first, followed by we are greeting Sebastian Ramrous with a time delay of two seconds and finally we are greeting Guido Vendorosa with a time delay of three seconds. So to visualize the program workflow, you can check how it is. So we had three requests here as well and in function one we were executing our program with a time delay of three seconds, function two, two seconds. So in this case with asynchronous execution and when we are using concurrent programming you can see that once a function three is being completed, it is not waiting for anything and it is not having the long time delay it had in the earlier case. So it is able to be executed right after with a one second delay. So it is executing after sometime in one second once the request has been started and then we are getting the corresponding results for response two and response one as well. So if you look at the Python's threading module, Python's threading module suffers some limitation. So some of the limitations are since threads are accessing the same shared state. So if both threads are accessing the same variable then it can update the same values and it can cause a race condition. So that is bad for us. In order to avoid race conditions some of the methods in threading live like logs etc but even then also there are same limitations with it like certain tasks may not be run in case of certain threads. So in order to solve the problem of certain tasks not being able to be run and to provide a standardized implementation of concurrency in C Python the Async.io module was being introduced as part of C Python I think from the Python 3.4 onwards and with Async.io module we are able to use Python to use cooperative multitasking to smartly run all the tasks and threads. It is using Async and awaits index. So when we are looking at Async.io module we have mainly four important concepts to look at one is the coroutines, other is the event loops, three is the tasks and four is the futures. So let's look at it one by one. So a coroutine is a special function which can suspend and resume execution they work like lightweight threads and it's a type of special function which is always being defined with the Async.defs index. And only within a coroutine we can use the available in the Async.io function. Then there is the concept of event loops. So event loop is like an indefinite loop which calls all the registered coroutines in some order until they all terminate. So what event loop is doing is like it is the real reason behind our programs being able to be run concurrently. So it's just a queue based on the coroutine function. If the corresponding coroutines need to be switched away and all event loops are the real reason for this. And you can run event loops in C Python using the Async.io.run module and from Python 3.7 onwards it's applicable. And if you want to run there is also something called UV loop which is a production implementation of the C Python implementation and it's almost giving 8x improvement in the performance compared to the normal Async.io.UV loop. So if you are using something for the production you definitely need to use UV loops. Then there is a concept of tasks. So task schedule is a coroutine in an event loop and you can wrap the coroutine to create a task with Async.io.CreateTaskMetal and you can run multiple tasks together concurrently using the Async.io.ContainMetal. So this is how this is all about tasks and then there is another concept of futures. So what futures is like it's providing in Async.io a framework which is available in multiple languages like in JavaScript where we have something to connect the producers and the consumers together and to wait for our execution to be completed. So futures is something in the C Python which is in the Async.io module which is used to bridge the low level callback base code to the high level Async.io. and it's providing the function of beating. So now it's an exciting moment for all of us because we are now using the Async.io. Async.io and the wait syntax for the first time in our talk and you can see that we have used the Async.dev main function to create a coroutine and we are using the Task1, Task2 and Task3 to create the task and we are calling the greet after function which we had used earlier. The only difference with this time is we are using and we are not using the time.sleep method but we are using the sleep method in the Async.io itself because certain libraries in Async.io are not compatible with the synchronous code so you need to use asynchronous libraries for that. Then finally we are running all the three tasks concurrently with the Async.io.gather task and finally you are calling the await to wait till all the tasks are being completed. Now in the main function we are calling we are using the Async.io.run function to run the event loop and to run this coroutine so that we are able to execute the coroutine function. So now you can see that we are getting the results as expected. We are now greeting Luciano, Ramaldo first and Sebastian, Ramres, second even though they were and we are now able to run our program concurrently. So concurrently now you can see that our program is ending in three seconds compared to six seconds and it may not be just a two-experiment in performance but for any input or to want problems which requires a lot of waiting it would be when we are using Async.io So now let's look at JavaScript and how we can implement some of the Async and await syntax in JavaScript. So this is an example of a sample program in JavaScript and you can see that I don't think you don't need to go into the details of the program but we are finding the prime numbers of one million numbers then followed by thousand and then ten so obviously since the program is being running in a potential order we are able to we are able to get the results only after a long time delay. So we need to wait for a long time. Let me have a sip of water. In JavaScript there are mainly three ways to write concurrent code so it can be done using the it can be done using callbacks, promises and Async and await syntax. So we will not be diving too much into the code for callbacks and promises so in callbacks callbacks is a way to invoke a function within another function. Within the callbacks if we are like writing our conditions properly we are able to ensure that our code is running asynchronously with callbacks but to program in callbacks it's difficult and now no one use callbacks because error handling is difficult and you may have heard about callback health because in any program any part of the function that ever can happen and it's difficult to spot it. Then there is the concept of promises so usually usually in any kind of programming languages there are two types of code one which is returning a result and another which is like producing the corresponding results. So promises are a way in JavaScript to link the prompt producers and the consumers together. So it's like an email newsletter which lets all the subscribers know when the result is ready for the conception and you can use the promises if you are using with a producer you have the resolve and reject keywords to get the corresponding values and if you are using if you are using consumers you can use promise chaining with operators like then catch etc. So now let's look at async and await syntax in keys of JavaScript. So under the hood actually async and await is being implemented with promises in JavaScript promises itself but it's a clean way to avoid the promise chaining promise chaining. So what async keyword is actually providing is it is allowing a function to return a corresponding promise and also secondly it is so it can either return a promise directly or inside it it can be called the awaitables like awaitables. What await keyword is actually doing is you can return the result of the promise once it is being settled with await keyword. So once a promise is being defined and if it is returning the promise only so it is like an indicator like which we have when we had discussed in the earlier condition initially in the layman's condition or it can throw an error or an exception when we are using await keyword. So now let's look at the sample code. So as an exercise for all of you you can read this code for 5 seconds and you can can you guess what the program will be printing after 5 seconds and you can comment in the chat box. So I hope you may have checked it out by now and got the answer. So we are having an async function called say hello here and what the function is doing is we are printing hello hello first and we are using a wait function and in the wait function what we are doing is we are returning the corresponding we are returning a promise with it. So what the if you look at the promise it is having the result and reject keywords and inside it there is a settimeout function. So if you are new to JavaScript settimeout to say function which like running a running minimum after 4 seconds and like if it is being really after running like it is going to print after 4 seconds what it is it is actually a call back we will look into it more detail in the coming slides and after if after the wait function is being called you can call the console.log world to print the world function. So I hope you all may have guessed the output like this way. So we are printing hello hello Patanastras thanks for waiting world. So you may be guessing why so let me explain this and why JavaScript is behaving like this with the slides from a wonderful talk by what the heck is when looked by Philip Roberts so I highly recommend you to check it out after this talk because it's something it's an amazing talk I highly recommend you to check it out. So you can see the source code we are having we are taking the console.log high then we are calling the corresponding settimeout function and after that we are calling jsconvue and in the so first we are going to the stack and printing high first when we are calling the settimeout function the settimeout function is me initially been going to the stack since it is calling a callback which needs to be waited for 5 seconds it's going to the corresponding queues by the same time by that time you can see that jsconvue is being printed correspondingly since javascript in runtime is asynchronous in nature and javascript like in webdisk asynchronous in nature or even in notjs also asynchronous in nature so jsconvue is also printed and after 5 seconds even loop is like printing the corresponding result of the task queue and after 5 seconds it's being pushed to the stack and we are getting the corresponding message with log there so this is how with the concept of even loops and using stacks and queues javascript is able to run asynchronously in the runtime so I hope you all got it so just to summarize what we have discussed so far we have discussed that we can run concurrent code in both python and javascript using threading and async.io so I think the stable summarizes everything perfectly so now let's look where async and await is useful so it's highly useful in case of massive scaling so without burning your pockets it's helpful to deal when you have a lot of users and you want to scale compared to other approaches like threading or multiprocessing and it's used to build high performance web frameworks dealing with a lot of input output operations like jpi, tonado, ars, ttp, coart etc so I hope you all have heard from Sebastian itself that how useful async.io function is and with concurrency only they are able to get almost great performance in fast api so now let's look at some of the common mistakes which we can make as a beginner when we are using async and await I have made all these mistakes and so I guess it will be easy for all of you to not repeat these mistakes so the first mistake can be we have a function f and we are having a promise which is resolving just a number and we are awaiting the result with that promise value so if you run this program you will see that you will get a syntax error so the reason is because we are awaiting without an async function so any async, if you want to use awaitables you need to always define it inside an async function only async await will be runable the next common mistake which you can make when you are starting is using async function without coroutines so async is a way to write coroutines but we are not awaiting or anything so in python this is going to throw a syntax error but and if you want to debug more you can use the python flags for async or debugging you know just imagine this like you async and await as a bullet for a bullet and imagine that if you have a bullet and if you are just keeping it in your watch instead of going for a write this is the exact scenario when you are using an async function without the awaitables or creating a corresponding task so another common mistake is to write blocking code so and you can all check out this piece of code and can you guess what will the output be in this case let me have a simple photo so you can see that we are calling task1, task2 and and we are greeting and we are using the async keyword and we are using the way to wait for something and you know we are using async.io.sleep as I said earlier itself for implementing in concurrently but you can see that this program is perfectly equivalent to our normal concurrent code so this is why this is why it is very common to this is why it is something which we all make and so you need to the mistake which we had here was you know we were awaiting all the time so that it was executing synchronously instead of being running concurrently so in order to fix that mistake we could have used async.io.gather method to run all these free tasks concurrently another mistake which we have made is like you know never run blocking code like read after function it was waiting for 10 seconds it was queuing up our queue right like our remand loop queue it was queuing up so don't use functions like that in our code so with that I would like to conclude by reminding you all of you to say that you know async and await is a great way to you know for input or bond calls let it be any database calls, api servers etc and if you want to build a wonderful web api frameworks which where you have a lot of network latency web api has a lot of network latency async.io is any frameworks based on async and await is a good tool and in both python and javascript it is having the similar similar syntax yesterday Sebastian Rambas mentioned some wonderful people who are behind these frameworks so I would like to tank all of them so these are the wonderful people behind implementing this feature and I would like to share some reference as well so I know a 30 minute talk is definitely not good enough to cover this talk so if you want to implement look into more into it like you can check out the mtn docs or async await in case of javascript and in python if you are interested you can check out the import async.io series in htp by lukas lenka so with that I would like to conclude my talk by offering tributes to arith chenwal he was a open source contributor in fosacea a wonderful human being thank you everyone and you can find the slides alright thank you