Python sleep (): How to add latency to Part I (Translated)

Tram Ho

Have you ever needed your program to wait for something? Often you want the code to be processed as quickly as possible. But sometimes letting code “sleep” is exactly what you care about.

For example, you can use sleep() to create latency in your program. You may need to wait for a file to be uploaded or downloaded, or wait for a chart to load or draw on the screen. You may even need to pause calls to a web API, or between queries to the database.

In this tutorial, you will learn how to add calls to sleep() to:

  • time.sleep()
  • Decorator
  • Thread
  • Async IO
  • Graphical User Interface

Note this article for intermediate programmers!

Adding a Python sleep() Call With time.sleep()

Python has built-in support for putting your program to sleep. The sleep() function of module time is used to pause the thread’s thread processing for as long as you want.

Here is an example of how to use it:

If you run the above code in the console, you will have to wait a bit before you want to add a new statement.

Note : In Python 3.5, core developers changed the behavior of time.sleep() a bit. The call to sleep() will last at least the number of seconds you pass in, even if the sleep is broken by a signal. However, this will not be true if the signal itself raises an exception.

You can check how long the sleep lasts using the timeit module:

Here, you run the timeit module with the -n parameter – the number of times the command will be run. You can see that the timeit ran the statement 3 times and the best runtime was 3 seconds, which is exactly what we expected.

The default number of times to run code for timeit is 1,000,000. If you run the above code with -n default, the terminal will hang for approximately 34 days! timeit module also has other options when running the command, you can refer to here .

Try creating something more realistic! A system administrator needs to know when one of their websites “dies”. You want to be able to check the system status on a regular basis but you cannot query the web server continuously or that may affect performance. One way to do this is to use sleep() :

Here you define the uptime_bot() function that takes a URL as an input parameter. This function will then try to open that URL with urllib . If there is an HTTPError or URLError , the program catches and prints the error. (In fact, you might log an error and send an email to the website’s owner or administrator.)

If no error occurred, the program will notify that everything is fine. No matter what happens, your program will sleep for about 60 seconds. This means you only visit the website once a minute. The link in the example is dead, so the program prints the following line in the console every minute:

Please update the code with a live URL like https://google.com . Then run the program again and see that everything looks better. You can try updating the code to email or log errors. For more details on these issues, you can learn Sending Emails With Python and Logging in Python .

Source: https://realpython.com/python-sleep/

Share the news now

Source : Viblo