黑松山资源网 Design By www.paidiu.com
1、添加一个任务
task2 = visit_url('http://another.com', 3) asynicio.run(task2)
2、这 2 个程序一共消耗 5s 左右的时间。并没有发挥并发编程的优势
import asyncio import time async def visit_url(url, response_time): """访问 url""" await asyncio.sleep(response_time) return f"访问{url}, 已得到返回结果" async def run_task(): """收集子任务""" task = visit_url('http://wangzhen.com', 2) task_2 = visit_url('http://another', 3) await asyncio.run(task) await asyncio.run(task_2) asyncio.run(run_task()) print(f"消耗时间:{time.perf_counter() - start_time}")
3、如果是并发编程,这个程序只需要消耗 3s,也就是task2的等待时间。
要想使用并发编程形式,需要把上面的代码改一下。asyncio.gather 会创建 2 个子任务,当出现 await 的时候,程序会在这 2 个子任务之间进行调度。
async def run_task(): """收集子任务""" task = visit_url('http://wangzhen.com', 2) task_2 = visit_url('http://another', 3) await asynicio.gather(task1, task2)
实例扩展:
import asyncio from threading import Thread async def production_task(): i = 0 while True: # 将consumption这个协程每秒注册一个到运行在线程中的循环,thread_loop每秒会获得一个一直打印i的无限循环任务 asyncio.run_coroutine_threadsafe(consumption(i), thread_loop) # 注意:run_coroutine_threadsafe 这个方法只能用在运行在线程中的循环事件使用 await asyncio.sleep(1) # 必须加await i += 1 async def consumption(i): while True: print("我是第{}任务".format(i)) await asyncio.sleep(1) def start_loop(loop): # 运行事件循环, loop以参数的形式传递进来运行 asyncio.set_event_loop(loop) loop.run_forever() thread_loop = asyncio.new_event_loop() # 获取一个事件循环 run_loop_thread = Thread(target=start_loop, args=(thread_loop,)) # 将次事件循环运行在一个线程中,防止阻塞当前主线程 run_loop_thread.start() # 运行线程,同时协程事件循环也会运行 advocate_loop = asyncio.get_event_loop() # 将生产任务的协程注册到这个循环中 advocate_loop.run_until_complete(production_task()) # 运行次循环
黑松山资源网 Design By www.paidiu.com
广告合作:本站广告合作请联系QQ:858582 申请时备注:广告合作(否则不回)
免责声明:本站资源来自互联网收集,仅供用于学习和交流,请遵循相关法律法规,本站一切资源不代表本站立场,如有侵权、后门、不妥请联系本站删除!
免责声明:本站资源来自互联网收集,仅供用于学习和交流,请遵循相关法律法规,本站一切资源不代表本站立场,如有侵权、后门、不妥请联系本站删除!
黑松山资源网 Design By www.paidiu.com
暂无评论...
更新日志
2024年07月03日
2024年07月03日
- 群星.2011《高雄国际Hi-End音响大展纪念发烧金碟》[WAV分轨]
- 康康1999-催泪[阿尔法][WAV+CUE]
- 和守望先锋太像?《星鸣特攻》开发商:我们与众不同
- 速通玩家有福了!夏季游戏速通大会正在火热进行中
- 《使命召唤》新处决动作曝光 或联动《死侍3》?
- 《陈百强出道35周年 文质翩翩》4CD[WAV+CUE][1.8GB]
- 邓丽君《思君集》3CD[WAV+CUE][1.2GB]
- 华语流行S.H.E《12CD》2001~2010[APE+CUE][3.7GB]
- 北风之望饮品怎么调配?饮品调酒配方分享
- 原画壁纸及美图第254期,无水印可自取
- 原画壁纸及美图第255期,无水印可自取
- 群星.1991-宝丽金冬日浓情【宝丽金】【WAV+CUE】
- 胡杨林.2006-香水有毒【太格印象】【WAV+CUE】
- 陈晓东.2002-从未忘记2CD【环球】【WAV+CUE】
- 绝区零珂蕾妲阵容怎么搭配好 珂蕾妲队伍搭配攻略