当前位置: 首页 > Python编程 > Python编程实战技能 > Python编程技术分享 > 深究Python中的asyncio库-线程同步

深究Python中的asyncio库-线程同步

发布时间:2020年09月27日 10:18:43 来源: 点击量:952

【摘要】前面的代码都是异步的,就如sleep,需要用asyncio sleep而不是阻塞的time sleep,如果有同步逻辑,怎么利用asyncio实现并发呢?答案是用run

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

同步代码

def a():
    time.sleep(1)
    return 'A'
async def b():
    await asyncio.sleep(1)
    return 'B'
def show_perf(func):
    print('*' * 20)
    start = time.perf_counter()
    asyncio.run(func())
    print(f'{func.__name__} Cost: {time.perf_counter() - start}')
async def c1():
    loop = asyncio.get_running_loop()
    await asyncio.gather(
        loop.run_in_executor(None, a),
        b()
    )
In : show_perf(c1)
********************
c1 Cost: 1.0027242230000866

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

async def a():
    time.sleep(1)
    return 'A'

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

async def c():
    loop = asyncio.get_running_loop()
    return await loop.run_in_executor(None, a)

大家理解了吧?

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

In : loop._default_executor
Out: <concurrent.futures.thread.ThreadPoolExecutor at 0x112b60e80>

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

async def c3():
    loop = asyncio.get_running_loop()
    with concurrent.futures.ProcessPoolExecutor() as e:
        print(await asyncio.gather(
            loop.run_in_executor(e, a),
            b()
        ))
In : show_perf(c3)
********************
['A', 'B']
c3 Cost: 1.0218078890000015

下一节:深究Python中的asyncio库-线程池

分享到: 编辑:wangmin

就业培训申请领取
您的姓名
您的电话
意向课程
点击领取

环球青藤

官方QQ

扫描上方二维码或点击一键加群,免费领取大礼包,加群暗号:青藤。 一键加群

绑定手机号

应《中华人民共和国网络安全法》加强实名认证机制要求,同时为更加全面的体验产品服务,烦请您绑定手机号.

预约成功

本直播为付费学员的直播课节

请您购买课程后再预约

环球青藤移动课堂APP 直播、听课。职达未来!

安卓版

下载

iPhone版

下载
环球青藤官方微信服务平台

刷题看课 APP下载

免费直播 一键购课

代报名等人工服务

课程咨询 学员服务 公众号

扫描关注微信公众号

APP

扫描下载APP

返回顶部