首页IT科技python for i in range()用法(深究Python中的asyncio库-线程同步)

python for i in range()用法(深究Python中的asyncio库-线程同步)

时间2025-05-23 13:28:27分类IT科技浏览2843
导读:前面的代码都是异步的,就如sleep,需要用asyncio.sleep而不是阻塞的time.sleep,如果有同步逻辑,怎么利用asyncio实现并发呢?答案是用run_in_executor。在一开始我说过开发者创建 Future 对象情况很少,主要是用run_in_executor,就是让同步函数在一个执行器( executo...

前面的代码都是异步的,就如sleep,需要用asyncio.sleep而不是阻塞的time.sleep,如果有同步逻辑,怎么利用asyncio实现并发呢?答案是用run_in_executor。在一开始我说过开发者创建 Future 对象情况很少,主要是用run_in_executor,就是让同步函数在一个执行器( executor)里面运行。

同步代码

defa(): time.sleep(1) returnA asyncdefb(): awaitasyncio.sleep(1) returnB defshow_perf(func): print(**20) start=time.perf_counter() asyncio.run(func()) print(f{func.__name__}Cost:{time.perf_counter()-start}) asyncdefc1(): loop=asyncio.get_running_loop() awaitasyncio.gather( loop.run_in_executor(None,a), b() ) In:show_perf(c1) ******************** c1Cost:1.0027242230000866

可以看到用run_into_executor可以把同步函数逻辑转化成一个协程,且实现了并发。这里要注意细节,就是函数a是普通函数,不能写成协程,下面的定义是错误的,不能实现并发:

asyncdefa(): time.sleep(1) returnA

因为 a 里面没有异步代码,就不要用async def来定义。需要把这种逻辑用loop.run_in_executor封装到协程:

asyncdefc(): loop=asyncio.get_running_loop() returnawaitloop.run_in_executor(None,a)

大家理解了吧?

loop.run_in_executor(None, a)这里面第一个参数是要传递concurrent.futures.Executor实例的,传递None会选择默认的executor:

In:loop._default_executor Out:<concurrent.futures.thread.ThreadPoolExecutorat0x112b60e80>

当然我们还可以用进程池,这次换个常用的文件读写例子,并且用:

asyncdefc3(): loop=asyncio.get_running_loop() withconcurrent.futures.ProcessPoolExecutor()ase: print(awaitasyncio.gather( loop.run_in_executor(e,a), b() )) In:show_perf(c3) ******************** [A,B] c3Cost:1.0218078890000015

创心域SEO版权声明:以上内容作者已申请原创保护,未经允许不得转载,侵权必究!授权事宜、对本内容有异议或投诉,敬请联系网站管理员,我们将尽快回复您,谢谢合作!

展开全文READ MORE
chatting中文翻译(【ChatGPT】ChatGPT 能否取代程序员?) 海带网官网(海带软件分享——Office 2021全家桶安装教程(附报错解决方法))