+ 2
âasyncâ keyword
Hi- I have seen some code on GitHub for python, and it has the async keyword before the def function keyword, so i was wondering what this meant as i am still like 15 percent done with py intermediate â¤ď¸
8 Answers
+ 3
interlinked keyword => git, git hub, py, python.
async => asynchronous.,
+ 2
leo, using the async keyword like this:-
async def func():
means that this is an asynchronous function (or a coroutine).
Unlike normal functions, an async function can pause its execution at certain points and let other tasks run in the meantime, before continuing later.
See, Python, in normal cases, is executed synchronously i.e., line by line. This is fine for many tasks, but it can cause problems when your program needs to wait for something slow because, in such a fase, the program would freeze until the slow task finishes. With async code, on the other hand, Python can switch to other tasks while itâs waiting, thereby making your program faster.
Basically, with async code the program kewps running while it is waiting, instead of freezing.
Feel free to let mw know if you need further clarification.
Thank you.
+ 1
how would i freeze and asynchronous function
+ 1
leo, do you want to make an asynchronous function behave like a synchronous one?
In that case, there are primarily two ways to do so.
One approach is to freeze the code from outside the async function by calling it synchronously using asyncio.run or loop.run_until_complete.
If you wish to freeze the code from inside the async function, you can deliberately insert blocking code that prevents the async function from yielding back control to the event loop thereby making it behave synchronously.
Also, note that if an async def contains no await inside it, and no other suspension point, when it is executed it will run to completion without yielding control i.e. it will behave synchronously.
Feel free to let me know should you have further questions.
Thank you.
0
I dont know abonut it
0
Hello, Arsala Afghan and welcome to Sololearn!
I can see that you have currently enrolled yourself for the "Introduction to Python" course available here. That means you're on the right track! If you pursue the couses diligently, I'm sure that you shall master Python eventually.
Let us know if you need recommendations for external sources.
Keep learning, keep coding!
Thank you.
0
Send me your facebook or whatsapp number
0
Arsala Afghan, I understand that you are new to Sololearn and may not be fully aware of the community guidelines.
Asking for or sharing personal information is not desirable and hence, goes against the community guidelines. I request you to kindly abide by the rules and post appropriate content in the Question and Answers forum, Comments and Post sections.
And last, but not the least, this is a self-learning platform, not social media. So, if you need any help of face any problem, create a thread here and wait for answers from the community.
Thank you.