Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

like image 402
InfoLearner Avatar asked Dec 02 '20 15:12

InfoLearner


2 Answers

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):

  • https://docs.python.org/3.7/library/asyncio-sync.html
  • https://docs.python.org/3.9/library/asyncio-sync.html
like image 87
mkrieger1 Avatar answered Oct 28 '22 01:10

mkrieger1


there should be no functional difference

BUT the latter was removed from python 3.9 see at the bottom of the page https://docs.python.org/3/library/asyncio-sync.html

Changed in version 3.9: Acquiring a lock using await lock or yield from lock and/or with statement (with await lock, with (yield from lock)) was removed. Use async with lock instead.

like image 32
Derte Trdelnik Avatar answered Oct 28 '22 02:10

Derte Trdelnik