summaryrefslogtreecommitdiffstats
path: root/Lib/asyncio/locks.py
diff options
context:
space:
mode:
authorAndrew Svetlov <andrew.svetlov@gmail.com>2017-12-08 22:23:48 (GMT)
committerGitHub <noreply@github.com>2017-12-08 22:23:48 (GMT)
commit5f841b553814969220b096a2b4f959b7f6fcbaf6 (patch)
treeb48ea916d9585efa9bf7ff370b50c4e2dfb30247 /Lib/asyncio/locks.py
parentede157331b4f9e550334900b3b4de1c8590688de (diff)
downloadcpython-5f841b553814969220b096a2b4f959b7f6fcbaf6.zip
cpython-5f841b553814969220b096a2b4f959b7f6fcbaf6.tar.gz
cpython-5f841b553814969220b096a2b4f959b7f6fcbaf6.tar.bz2
bpo-32193: Convert asyncio to async/await usage (#4753)
* Convert asyncio/tasks.py to async/await * Convert asyncio/queues.py to async/await * Convert asyncio/test_utils.py to async/await * Convert asyncio/base_subprocess.py to async/await * Convert asyncio/subprocess.py to async/await * Convert asyncio/streams.py to async/await * Fix comments * Convert asyncio/locks.py to async/await * Convert asyncio.sleep to async def * Add a comment * Add missing news * Convert stubs from AbstrctEventLoop to async functions * Convert subprocess_shell/subprocess_exec * Convert connect_read_pipe/connect_write_pip to async/await syntax * Convert create_datagram_endpoint * Convert create_unix_server/create_unix_connection * Get rid of old style coroutines in unix_events.py * Convert selector_events.py to async/await * Convert wait_closed and create_connection * Drop redundant line * Convert base_events.py * Code cleanup * Drop redundant comments * Fix indentation * Add explicit tests for compatibility between old and new coroutines * Convert windows event loop to use async/await * Fix double awaiting of async function * Convert asyncio/locks.py * Improve docstring * Convert tests to async/await * Convert more tests * Convert more tests * Convert more tests * Convert tests * Improve test
Diffstat (limited to 'Lib/asyncio/locks.py')
-rw-r--r--Lib/asyncio/locks.py42
1 files changed, 19 insertions, 23 deletions
diff --git a/Lib/asyncio/locks.py b/Lib/asyncio/locks.py
index 750c435..aa6ed3e 100644
--- a/Lib/asyncio/locks.py
+++ b/Lib/asyncio/locks.py
@@ -66,20 +66,21 @@ class _ContextManagerMixin:
yield from self.acquire()
return _ContextManager(self)
+ async def __acquire_ctx(self):
+ await self.acquire()
+ return _ContextManager(self)
+
def __await__(self):
# To make "with await lock" work.
- yield from self.acquire()
- return _ContextManager(self)
+ return self.__acquire_ctx().__await__()
- @coroutine
- def __aenter__(self):
- yield from self.acquire()
+ async def __aenter__(self):
+ await self.acquire()
# We have no use for the "as ..." clause in the with
# statement for locks.
return None
- @coroutine
- def __aexit__(self, exc_type, exc, tb):
+ async def __aexit__(self, exc_type, exc, tb):
self.release()
@@ -156,8 +157,7 @@ class Lock(_ContextManagerMixin):
"""Return True if lock is acquired."""
return self._locked
- @coroutine
- def acquire(self):
+ async def acquire(self):
"""Acquire a lock.
This method blocks until the lock is unlocked, then sets it to
@@ -170,7 +170,7 @@ class Lock(_ContextManagerMixin):
fut = self._loop.create_future()
self._waiters.append(fut)
try:
- yield from fut
+ await fut
self._locked = True
return True
except futures.CancelledError:
@@ -251,8 +251,7 @@ class Event:
to true again."""
self._value = False
- @coroutine
- def wait(self):
+ async def wait(self):
"""Block until the internal flag is true.
If the internal flag is true on entry, return True
@@ -265,7 +264,7 @@ class Event:
fut = self._loop.create_future()
self._waiters.append(fut)
try:
- yield from fut
+ await fut
return True
finally:
self._waiters.remove(fut)
@@ -307,8 +306,7 @@ class Condition(_ContextManagerMixin):
extra = '{},waiters:{}'.format(extra, len(self._waiters))
return '<{} [{}]>'.format(res[1:-1], extra)
- @coroutine
- def wait(self):
+ async def wait(self):
"""Wait until notified.
If the calling coroutine has not acquired the lock when this
@@ -327,7 +325,7 @@ class Condition(_ContextManagerMixin):
fut = self._loop.create_future()
self._waiters.append(fut)
try:
- yield from fut
+ await fut
return True
finally:
self._waiters.remove(fut)
@@ -336,13 +334,12 @@ class Condition(_ContextManagerMixin):
# Must reacquire lock even if wait is cancelled
while True:
try:
- yield from self.acquire()
+ await self.acquire()
break
except futures.CancelledError:
pass
- @coroutine
- def wait_for(self, predicate):
+ async def wait_for(self, predicate):
"""Wait until a predicate becomes true.
The predicate should be a callable which result will be
@@ -351,7 +348,7 @@ class Condition(_ContextManagerMixin):
"""
result = predicate()
while not result:
- yield from self.wait()
+ await self.wait()
result = predicate()
return result
@@ -432,8 +429,7 @@ class Semaphore(_ContextManagerMixin):
"""Returns True if semaphore can not be acquired immediately."""
return self._value == 0
- @coroutine
- def acquire(self):
+ async def acquire(self):
"""Acquire a semaphore.
If the internal counter is larger than zero on entry,
@@ -446,7 +442,7 @@ class Semaphore(_ContextManagerMixin):
fut = self._loop.create_future()
self._waiters.append(fut)
try:
- yield from fut
+ await fut
except:
# See the similar code in Queue.get.
fut.cancel()