“带锁异步”和“带等待锁”有什么区别?
我见过两种获取asyncio Lock 的方法:
async def main(lock):
async with lock:
async.sleep(100)
和
async def main(lock):
with await lock:
async.sleep(100)
它们之间有什么区别?
回答
第二种形式with await lock自 Python 3.7 起已弃用,并在 Python 3.9 中删除。
使用 Python 3.7 运行它会给出以下警告:
DeprecationWarning: 'with await lock' 被弃用,使用'async with lock'代替
来源(滚动到底部):
- https://docs.python.org/3.7/library/asyncio-sync.html
- https://docs.python.org/3.9/library/asyncio-sync.html