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

How to manually delay in Python

anonymity
anonymityOriginal
2019-05-27 13:57:425041browse

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.

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).

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.

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)

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!

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()

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

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!

Statement:
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