python中sleep函数用法演示的代码

下面的内容段是关于python中sleep函数用法演示的内容,希望对小伙伴们有较大用处。

#------------------------------------------------------------------------------

Name: sleep.py

Author: Kevin Harris

Last Modified: 02/13/04

Description: This Python script demonstrates how to use the sleep()

function.

#------------------------------------------------------------------------------

from time import sleep

print( “We’ll start off by sleeping 5 seconds” )

sleep( 5 )

print( “Ok, time to wake up!” )

wait_time = int( input( "How much longer would you like to sleep? " ) )

while wait_time > 0:
print( “Ok, we’ll sleep for " + str(wait_time) + " more seconds…” )
sleep( wait_time )
wait_time = int( input( "How much longer would you like to sleep? " ) )

print( “We’re done!” )

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
Python函数使用线程池有两种方式。第一种方式是使用第三方库threadpool,它提供了一个ThreadPool类来管理线程池。您可以通过创建ThreadPool对象,并使用makeRequests方法创建任务,然后将任务添加到线程池执行。最后,您可以使用wait方法等待所有任务完成。此外,您还可以定义回调函数来获取任务的结果。 另一种方式是使用Python 3引入的concurrent.futures.ThreadPoolExecutor库。您可以创建ThreadPoolExecutor对象,并使用submit方法提交任务给线程池。每个任务会返回一个Future对象,您可以使用这个对象的result方法获取任务的结果。 下面是使用这两种方式的示例代码: ```python # 使用threadpool import time import threadpool import threading def process(data): """消费者""" print("%s process to %s" % (threading.current_thread().getName(), data)) time.sleep(1) return data def callback(request, result): """回调函数,用于取回结果""" print(f"callback result = {result}") def main(): start_time = time.time() # 创建线程池 pool = threadpool.ThreadPool(3) # 创建任务 requests_ = threadpool.makeRequests(process, range(10), callback) # 创建任务 for req in requests_: pool.putRequest(req) pool.wait() print(f"{threading.current_thread().getName()} 用时:{time.time() - start_time}") if __name__ == '__main__': main() # 使用ThreadPoolExecutor from concurrent.futures import ThreadPoolExecutor def process(data): """消费者""" print("%s process to %s" % (threading.current_thread().getName(), data)) time.sleep(1) return data def main(): start_time = time.time() # 创建线程池 with ThreadPoolExecutor(3) as executor: # 提交任务给线程池 results = [executor.submit(process, i) for i in range(10)] # 获取任务的结果 for future in results: print(f"callback result = {future.result()}") print(f"{threading.current_thread().getName()} 用时:{time.time() - start_time}") if __name__ == '__main__': main() ``` 这些示例代码演示了如何在Python函数使用线程池,您可以根据自己的需求选择适合的方式进行使用。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* *2* [python 线程池使用](https://blog.csdn.net/weixin_45459224/article/details/126182031)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_1"}}] [.reference_item style="max-width: 50%"] - *3* [Python函数综合案例.md](https://download.csdn.net/download/qq_42431718/88241199)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_1"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值