]>
vault307.fbx.one Git - ir_remote.git/blob - primitives/condition.py
3 # Copyright (c) 2018-2020 Peter Hinch
4 # Released under the MIT License (MIT) - see LICENSE file
7 import uasyncio
as asyncio
12 # from primitives.condition import Condition
15 def __init__(self
, lock
=None):
16 self
.lock
= asyncio
.Lock() if lock
is None else lock
19 async def acquire(self
):
20 await self
.lock
.acquire()
23 # with await condition [as cond]:
25 await self
.lock
.acquire()
33 def __exit__(self
, *_
):
37 return self
.lock
.locked()
40 self
.lock
.release() # Will raise RuntimeError if not locked
42 def notify(self
, n
=1): # Caller controls lock
43 if not self
.lock
.locked():
44 raise RuntimeError('Condition notify with lock not acquired.')
45 for _
in range(min(n
, len(self
.events
))):
46 ev
= self
.events
.pop()
50 self
.notify(len(self
.events
))
53 if not self
.lock
.locked():
54 raise RuntimeError('Condition wait with lock not acquired.')
56 self
.events
.append(ev
)
59 await self
.lock
.acquire()
60 assert ev
not in self
.events
, 'condition wait assertion fail'
61 return True # CPython compatibility
63 async def wait_for(self
, predicate
):