答案是使用time.sleep()可让Python程序暂停执行,它通过操作系统调度实现非阻塞休眠,精度受系统影响,适用于常规延时;在异步编程中应改用asyncio.sleep()以避免阻塞事件循环,同时time模块还提供time.time()、perf_counter()、monotonic()等函数用于计时和时间格式化操作。

Python中让程序暂停执行,核心就是利用
time
sleep()
要让Python程序暂停,你只需要导入
time
time.sleep()
比如,你想让程序暂停5秒:
import time
print("程序开始执行...")
time.sleep(5) # 程序会在这里暂停5秒
print("程序暂停了5秒后继续执行。")或者,如果你需要更精细的控制,比如暂停半秒:
立即学习“Python免费学习笔记(深入)”;
import time
print("开始计时...")
time.sleep(0.5) # 暂停0.5秒
print("0.5秒过去了。")这个函数在很多场景下都非常有用。我记得刚开始写爬虫的时候,就经常用
sleep
time.sleep()
time.sleep()
当你调用
time.sleep(seconds)
seconds
seconds
seconds
所以,
time.sleep()
至于精确性,这其实是一个比较复杂的问题。在大多数现代操作系统上,
time.sleep()
seconds
sleep
举个例子,你
sleep(0.001)
time.sleep()
time.sleep()
time.sleep()
asyncio
time.sleep()
想象一下,你有一个
asyncio
time.sleep(5)
sleep
所以,在异步编程中,我们绝不能直接使用
time.sleep()
asyncio
asyncio.sleep()
asyncio.sleep()
awaitable
asyncio.sleep()
来看个对比:
使用 time.sleep()
import asyncio
import time
async def task_a():
print("Task A: 开始...")
time.sleep(2) # 阻塞整个事件循环
print("Task A: 结束。")
async def task_b():
print("Task B: 开始...")
await asyncio.sleep(0.1) # 即使这里是异步sleep,也会被上面的time.sleep阻塞
print("Task B: 结束。")
async def main_blocking():
await asyncio.gather(task_a(), task_b())
print("--- 使用 time.sleep() 阻塞 ---")
# asyncio.run(main_blocking()) # 运行会发现 task_b 并没有在 task_a 暂停时执行(注:如果你真的运行上面的
main_blocking()
Task B
Task A
time.sleep(2)
使用 asyncio.sleep()
import asyncio
async def task_async_a():
print("Task Async A: 开始...")
await asyncio.sleep(2) # 非阻塞暂停
print("Task Async A: 结束。")
async def task_async_b():
print("Task Async B: 开始...")
await asyncio.sleep(0.1) # 非阻塞暂停
print("Task Async B: 结束。")
async def main_non_blocking():
await asyncio.gather(task_async_a(), task_async_b())
print("\n--- 使用 asyncio.sleep() 非阻塞 ---")
asyncio.run(main_non_blocking())运行
main_non_blocking()
Task Async A: 开始...
Task Async B: 开始...
Task Async B: 结束。
Task Async A: 结束。
所以,记住一点:在异步函数(用
async def
await asyncio.sleep()
time.sleep()
time
time
sleep()
time.time()
import time
start_time = time.time()
# 模拟一些耗时操作
sum(range(10**7))
end_time = time.time()
print(f"操作耗时: {end_time - start_time:.4f} 秒")time.perf_counter()
time.time()
time.perf_counter()
import time
start_perf = time.perf_counter()
# 模拟一些短时操作
[x*x for x in range(10**5)]
end_perf = time.perf_counter()
print(f"高精度操作耗时: {end_perf - start_perf:.6f} 秒")time.monotonic()
import time
start_mono = time.monotonic()
time.sleep(1.2) # 暂停1.2秒
end_mono = time.monotonic()
print(f"单调时钟测量暂停时间: {end_mono - start_mono:.4f} 秒")time.ctime()
time.strftime()
time
time.ctime()
time.strftime()
import time
current_timestamp = time.time()
print(f"当前时间(ctime): {time.ctime(current_timestamp)}")
# 自定义格式:年-月-日 时:分:秒
print(f"当前时间(strftime): {time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(current_timestamp))}")time.localtime()
struct_time
strftime
这些函数共同构成了
time
以上就是python sleep函数如何暂停程序_python time.sleep()函数使用方法的详细内容,更多请关注php中文网其它相关文章!
python怎么学习?python怎么入门?python在哪学?python怎么学才快?不用担心,这里为大家提供了python速学教程(入门到精通),有需要的小伙伴保存下载就能学习啦!
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号