未加星标

Programiz: Python sleep()

字体大小 | |
[开发(python) 所属分类 开发(python) | 发布者 店小二04 | 时间 2019 | 作者 红领巾 ] 0人收藏点击收藏

python has a module namedtime which provides several useful functions to handle time-related tasks. One of the popular functions among them is sleep() .

The sleep() function suspends execution of the current thread for a given number of seconds.

Example 1: Python sleep() import time print("Printed immediately.") time.sleep(2.4) print("Printed after 2.4 seconds.")

Here's how this program works:

"Printed immediately" "Printed after 2.4 seconds"

As you can see from the above example, sleep() takes a floating-point number as an argument.

Before Python 3.5, the actual suspension time may be less than the argument specified to the time() function.

Since Python 3.5, the suspension time will be at least the seconds specified.

Example 2: Python create a digital clock import time while True: localtime = time.localtime() result = time.strftime("%I:%M:%S %p", localtime) print(result) time.sleep(1)

In the above program, we computed and printed the current local time inside the infinitewhile loop. Then, we suspended the execution for 1 second. Again, the current local time is computed and printed. This process goes on.

When you run the program, the output will be something like:

<samp>02:10:50 PM 02:10:51 PM 02:10:52 PM 02:10:53 PM 02:10:54 PM ... .. ...</samp>

Here is a slightly modified better version of the above program.

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

To learn more, visit digital clock in Python shell .

Multithreading in Python

Before talking about sleep() in multithreaded programs, let's talk about processes and threads.

A computer program is a collection of instructions. A process is the execution of those instructions.

A thread is a subset of the process. A process can have one or more threads.

Example 3: Python multithreading

All the programs above in this article are single-threaded programs. Here's an example of a multithreaded Python program.

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

When you run the program, the output will be something like:

<samp> Hello Hello Hi Hello Hi Hi </samp>

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

Note that, t1 and t2 runconcurrently and you might get different output.

Visit this page to learn more about Multithreading in Python .

time.sleep() in multithreaded programs

The sleep() function suspends execution of the current thread for a given number of seconds.

In case of single-threaded programs, sleep() suspends execution of the thread and process. However, the function suspends a thread rather than the whole process in multithreaded programs.

Example 4: sleep() in amultithreaded 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 above program has two threads. We have used time.sleep(0.5) and time.sleep(0.75) to suspend execution of these two threads for 0.5 seconds and 0.7 seconds respectively.

Recommended Reading: Python time.sleep() sleeps thread

本文开发(python)相关术语:python基础教程 python多线程 web开发工程师 软件开发工程师 软件开发流程

分页:12
转载请注明
本文标题:Programiz: Python sleep()
本站链接:https://www.codesec.net/view/628120.html


1.凡CodeSecTeam转载的文章,均出自其它媒体或其他官网介绍,目的在于传递更多的信息,并不代表本站赞同其观点和其真实性负责;
2.转载的文章仅代表原创作者观点,与本站无关。其原创性以及文中陈述文字和内容未经本站证实,本站对该文以及其中全部或者部分内容、文字的真实性、完整性、及时性,不作出任何保证或承若;
3.如本站转载稿涉及版权等问题,请作者及时联系本站,我们会及时处理。
登录后可拥有收藏文章、关注作者等权限...
技术大类 技术大类 | 开发(python) | 评论(0) | 阅读(38)