沈阳网站制作公司上海最好的网站设计公司
2026/6/9 17:23:20 网站建设 项目流程
沈阳网站制作公司,上海最好的网站设计公司,快速建站套餐,做斗图网站GIL锁#xff1a; 当python使用多线程的时候#xff0c; 每个线程通过请求这个锁获取运行权。 结束时归还 async/await/asyncio/gather/create_task/ThreadPool 理解asynciohttps://www.bilibili.com/video/BV1oa411b7c9?spm_id_from333.788.videopod.sectionsvd_sour…GIL锁当python使用多线程的时候 每个线程通过请求这个锁获取运行权。 结束时归还async/await/asyncio/gather/create_task/ThreadPool理解asynciohttps://www.bilibili.com/video/BV1oa411b7c9?spm_id_from333.788.videopod.sectionsvd_source8ca92588511fc633026e558331f021cb装饰的functioncoroutine object真正的并行和协程 2s执行结束async def main(): task1 asyncio.create_task(say_after(1, hello)) task2 asyncio.create_task(say_after(2, world)) print(start at, time.strftime(%Y-%m-%d %H:%M:%S, time.localtime())) await task1 await task2 print(end at, time.strftime(%Y-%m-%d %H:%M:%S, time.localtime()))串行 3s执行结束async def main(): print(start at, time.strftime(%Y-%m-%d %H:%M:%S, time.localtime())) await say_after(1, hello) await say_after(2, world) print(end at, time.strftime(%Y-%m-%d %H:%M:%S, time.localtime()))如果使用gather方式, 不需要单独create task再放进去伪协程async def main(): print(start at, time.strftime(%Y-%m-%d %H:%M:%S, time.localtime())) res asyncio.gather( say_after(1, hello), say_after_2(2, world) ) print(res) print(end at, time.strftime(%Y-%m-%d %H:%M:%S, time.localtime()))threadpoolwith ThreadPoolExecutor(max_workers2) as executor: # 存储Future对象与自定义数据的映射 future_to_params {} for task_id in range(1, 4): delay task_id # 任务1延迟1秒任务2延迟2秒任务3延迟3秒 future executor.submit(task, delay) future_to_params[future] (task_id, delay) # 关联任务ID和延迟 # 按完成顺序处理 for future in as_completed(future_to_params): task_id, delay future_to_params[future] try: result future.result() print(f任务ID{task_id}{result}) except Exception as e: print(f任务ID{task_id}执行失败{e})VizTracer:Viztracer开发者讲解https://www.bilibili.com/video/BV1d34y1C78W?spm_id_from333.788.player.switchvd_source8ca92588511fc633026e558331f021cbp2基础用法装pip包全局方式使用 python 1.py - viztracer 1.py内部函数方式使用with VizTracer(): func()Perfettogoogle 开源的traceJupyter中使用# 初始load %load_ext viztracer # 使用时 %%viztracer

需要专业的网站建设服务?

联系我们获取免费的网站建设咨询和方案报价,让我们帮助您实现业务目标

立即咨询