Python中的asyncio库-shield函数?针对这个问题,这篇文章详细介绍了相对应的分析和解答,希望可以帮助更多想解决这个问题的小伙伴找到更简单易行的方法。

shield

asyncio.shield,用它可以屏蔽取消操作。一直到这里,我们还没有见识过Task的取消。看一个例子:

In:loop=asyncio.get_event_loop()In:task1=loop.create_task(a())In:task2=loop.create_task(b())In:task1.cancel()Out:TrueIn:awaitasyncio.gather(task1,task2)SuspendingaSuspendingb---------------------------------------------------------------------------CancelledErrorTraceback(mostrecentcalllast)cell_nameinasync-def-wrapper()CancelledError:

在上面的例子中,task1被取消了后再用asyncio.gather收集结果,直接抛CancelledError错误了。这里有个细节,gather支持return_exceptions参数:

In:awaitasyncio.gather(task1,task2,return_exceptions=True)Out:[concurrent.futures._base.CancelledError(),'B']

可以看到,task2依然会执行完成,但是task1的返回值是一个CancelledError错误,也就是任务被取消了。如果一个创建后就不希望被任何情况取消,可以使用asyncio.shield保护任务能顺利完成。不过要注意一个陷阱,先看错误的写法:

In:task1=asyncio.shield(a())In:task2=loop.create_task(b())In:task1.cancel()Out:TrueIn:awaitasyncio.gather(task1,task2,return_exceptions=True)SuspendingaSuspendingbResumingbOut:[concurrent.futures._base.CancelledError(),'B']

可以看到依然是CancelledError错误,且协程a未执行完成,正确的用法是这样的:

In:task1=asyncio.shield(a())In:task2=loop.create_task(b())In:ts=asyncio.gather(task1,task2,return_exceptions=True)In:task1.cancel()Out:TrueIn:awaittsSuspendingaSuspendingbResumingaResumingbOut:[concurrent.futures._base.CancelledError(),'B']

可以看到虽然结果是一个CancelledError错误,但是看输出能确认协程实际上是执行了的。所以正确步骤是:

先创建 GatheringFuture 对象 ts

取消任务

await ts

asynccontextmanager

如果你了解Python,之前可能听过或者用过contextmanager ,一个上下文管理器。通过一个计时的例子就理解它的作用:

fromcontextlibimportcontextmanagerasyncdefa():awaitasyncio.sleep(3)return'A'asyncdefb():awaitasyncio.sleep(1)return'B'asyncdefs1():returnawaitasyncio.gather(a(),b())@contextmanagerdeftimed(func):start=time.perf_counter()yieldasyncio.run(func())print(f'Cost:{time.perf_counter()-start}')

timed函数用了contextmanager装饰器,把协程的运行结果yield出来,执行结束后还计算了耗时:

In:fromcontextmanagerimport*In:withtimed(s1)asrv:...:print(f'Result:{rv}')...:Result:['A','B']Cost:3.0052654459999992

大家先体会一下。在Python 3.7添加了asynccontextmanager,也就是异步版本的contextmanager,适合异步函数的执行,上例可以这么改:

@asynccontextmanagerasyncdefasync_timed(func):start=time.perf_counter()yieldawaitfunc()print(f'Cost:{time.perf_counter()-start}')asyncdefmain():asyncwithasync_timed(s1)asrv:print(f'Result:{rv}')In:asyncio.run(main())Result:['A','B']Cost:3.00414147500004

async版本的with要用async with,另外要注意yield await func()这句,相当于yield + await func()

PS: contextmanager 和 asynccontextmanager 最好的理解方法是去看源码注释

关于Python中的asyncio库-shield函数问题的解答就分享到这里了,希望以上内容可以对大家有一定的帮助,如果你还有很多疑惑没有解开,可以关注亿速云行业资讯频道了解更多相关知识。