Warm tip: This article is reproduced from serverfault.com, please click

What is the difference between "async with lock" and "with await lock"?

发布于 2020-12-02 15:55:40

I have seen two ways of acquiring the asyncio Lock:

async def main(lock):
  async with lock:
    async.sleep(100)

and

async def main(lock):
  with await lock:
    async.sleep(100)

What is the difference between them?

Questioner
InfoLearner
Viewed
0
mkrieger1 2020-12-03 00:04:15

The second form with await lock is deprecated since Python 3.7 and is removed in Python 3.9.

Running it with Python 3.7 gives this warning:

DeprecationWarning: 'with await lock' is deprecated use 'async with lock' instead

Sources (scroll to the bottom):