asyncio.sleep() method

HPaduB report abuse

I have seen that some programs use asyncio.sleep() method. It seems that it does the same as the time.sleep() method - waits for the specified number of seconds. Then what are the difference between them and why asyncio.sleep() can be needed if there is already time.sleep() functions?


PyAnto report abuse

Hi @HPaduB

The difference is that asycio's method is a non-blocking method, while time.sleep() is blocking. The blocking code pauses the execution of the program. So, when you call time.sleep(10), your script will wait 10 seconds and do nothing else. In the same time, asyncio.sleep(10) waits 10 seconds "in the background". This means that other work can be done by a program meanwhile.


Ifful report abuse

Good example, @PyAnto. I just want to add that you should place await keyword before asyncio.sleep() in order that all these things can work properly.

HPaduB report abuse

Thank you all for your explanations! I will try to play with blocking and non-blocking code to understand the topic better.

Add Answer

Need support?

Just drop us an email to ... Show more