Home > Backend Development > Python Tutorial > How to manually delay in Python

How to manually delay in Python

anonymity
Release: 2019-05-27 13:57:42
Original
5113 people have browsed it

I want to know how to add a time delay in a Python script.

import time
time.sleep(5)   # Delays for 5 seconds. You can also use a float value.
Copy after login

Here is another example, running approximately every minute:

import timewhile True:
    print("This prints once a minute.")
    time.sleep(60) # Delay for 1 minute (60 seconds).
Copy after login

You can use the sleep() function in the time module. It can adopt floating parameters for sub-second resolution.

from time import sleep
sleep(0.1) # Time in seconds.
Copy after login

How to manually delay in Python

How to manually delay in Python?

In a thread I recommend the sleep function:

>>> from time import sleep
>>> sleep(4)
Copy after login

This actually pauses the processing of the thread that the operating system calls it on, allowing other threads and processes to execute while sleeping.

Use it for this purpose, or simply to delay the execution of a function. For example:

>>> def party_time():
...     print('hooray!')
... 
>>> sleep(3); party_time()
hooray!
Copy after login

Print out Enter after 3 seconds.

Example of using sleep with multiple threads and processes

Again, sleep pauses your threads - it uses zero processing power.

To demonstrate, create a script like this (I first tried this in an interactive Python 3.5 shell, but the subprocess party_later couldn't find the function for some reason):

from concurrent.futures import ThreadPoolExecutor, ProcessPoolExecutor, as_completed
from time import sleep, time
def party_later(kind='', n=''):
    sleep(3)
    return kind + n + ' party time!: ' + __name__
def main():
    with ProcessPoolExecutor() as proc_executor:
        with ThreadPoolExecutor() as thread_executor:
            start_time = time()
            proc_future1 = proc_executor.submit(party_later, kind='proc', n='1')
            proc_future2 = proc_executor.submit(party_later, kind='proc', n='2')
            thread_future1 = thread_executor.submit(party_later, kind='thread', n='1')
            thread_future2 = thread_executor.submit(party_later, kind='thread', n='2')
            for f in as_completed([
              proc_future1, proc_future2, thread_future1, thread_future2,]):
                print(f.result())
            end_time = time()
    print('total time to execute four 3-sec functions:', end_time - start_time)
if __name__ == '__main__':
    main()
Copy after login

Example output from this script:

thread1 party time!: __main__
thread2 party time!: __main__
proc1 party time!: __mp_main__
proc2 party time!: __mp_main__
total time to execute four 3-sec functions: 3.4519670009613037
Copy after login

The above is the detailed content of How to manually delay in Python. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template