Function sleep () in Python

Module time in Python provides some very useful functions to handle time-related tasks. One of the most commonly used functions is sleep () .

The sleep () function in Python is used to stop executing the current thread in the number of seconds passed.

Example 1: Use sleep ()

 import time print ("In kết quả ra màn hình ngay lập tức.") time.sleep(3) print ("In kết quả ra màn hình sau 3s.") 

This method does not return any value but only the executable delay, which works like this:

  1. Execute the task to display the results "Print results to the screen immediately."
  2. Delay execution within 3 seconds.
  3. Continue to perform the task and display "Print results to the screen after 3 seconds."

Example 2: Create an electronic clock in Python

 import time while True: localtime = time.localtime() result = time.strftime("%I:%M:%S %p", localtime) print(result) time.sleep(1) 

In the above program, Quantrimang created and printed local time inside an infinite while loop. After printing the results, the executable will delay within 1 second and continue printing the current time. This process is continuous thanks to a while loop , forming an electronic clock in Python.

 11:58:31 AM 11:58:32 AM 11:58:33 AM 11:58:34 AM 11:58:35 AM 11:58:36 AM 11:58:37 AM 11:58:38 AM . . . 

Or another way of making electronic clocks:

 import time while True: localtime = time.localtime() result = time.strftime("%I:%M:%S %p", localtime) print(result, end="", flush=True) print("n", end="", flush=True) time.sleep(1) 

Multithreading in Python

Before talking about sleep () in multi-threaded programs, let's mention a little bit about Process and Thread.

  1. Process is the operation of a program.
  2. A thread is an operating step inside a process. A process can contain multiple threads within it.

Example 3: Python multithreading

 import threading def print_hello_three_times(): for i in range(3): print("Hello") def print_hi_three_times(): for i in range(3): print("Hi") t1 = threading.Thread(target=print_hello_three_times) t2 = threading.Thread(target=print_hi_three_times) t1.start() t2.start() 

Running the program, the output to the screen will look like this:

 Hello Hello Hi Hello Hi Hi 

The above program has two threads t1 and t2. These threads are run using the statements t1.start () and t2.start ().

Note that, t1 and t2 run simultaneously and you can get different output.

time.sleep () in multithreaded programs

The sleep () function pauses execution of the current thread for a certain number of seconds.

In the case of single threaded programs, sleep () pauses thread execution and processing. However, in multithreaded programs, this function only pauses a thread instead of the entire multithreaded process.

Example 4: sleep () in a multithreaded program

 import threading import time def print_hello(): for i in range(4): time.sleep(0.5) print("Hello") def print_hi(): for i in range(4): time.sleep(0.7) print("Hi") t1 = threading.Thread(target=print_hello) t2 = threading.Thread(target=print_hi) t1.start() t2.start() 

The result has the form:

 Hello Hi Hello Hi Hello Hello Hi Hi 

The above program has two threads. Here we used time.sleep (0.5) and time.sleep (0.75) to pause the execution of these two threads in 0.5 seconds and 0.7 seconds respectively.

Last lesson: Module time in Python

5 ★ | 1 Vote

May be interested

  • The function set () in PythonThe function set () in Python
    in this article, tipsmake.com will learn with you about set (), syntax, parameters and specific examples. invites you to read the track.
  • Help () function in PythonHelp () function in Python
    the built-in help () function in python is used to display documents and invoke the help system of modules, functions, classes, keywords ...
  • Sum () function in PythonSum () function in Python
    the built-in function sum () in python returns the sum of all numbers in iterable.
  • The slice () function in PythonThe slice () function in Python
    the slice () function in python returns a slice object that helps you determine how to cut an existing string.
  • The chr () function in PythonThe chr () function in Python
    in python, the chr () function returns a character (a string) from an integer that represents the unicode of the returned character.
  • The iter () function in PythonThe iter () function in Python
    continuing with the topic of python's built-in functions, the article will introduce you to the iter () function with the syntax, usage as well as specific examples. invites you to read the track.
  • The pow () function in PythonThe pow () function in Python
    the pow () function built into python returns the value of x with the power of y (xy). if there is a third parameter, the function returns x to the power y, the module z.
  • The reversed () function in PythonThe reversed () function in Python
    the reversed () function is one of the built-in functions in python, used to reverse the original string returning the iterator.
  • Built-in Python functionsBuilt-in Python functions
    in the previous article, you learned that python has a basic function type, one is a built-in function, two are user-defined functions. today, we will learn about a list of python's built-in functions.
  • Exec () function in PythonExec () function in Python
    the exec () function used to execute python programs dynamically can be string or object code. how does exec () function syntax, what parameters do it have, and how is it used? invites you to read the track.