]>
vault307.fbx.one Git - ir_remote.git/blob - primitives/semaphore.py
3 # Copyright (c) 2018-2020 Peter Hinch
4 # Released under the MIT License (MIT) - see LICENSE file
7 import uasyncio
as asyncio
11 # A Semaphore is typically used to limit the number of coros running a
12 # particular piece of code at once. The number is defined in the constructor.
14 def __init__(self
, value
=1):
16 self
._event
= asyncio
.Event()
18 async def __aenter__(self
):
22 async def __aexit__(self
, *args
):
24 await asyncio
.sleep(0)
26 async def acquire(self
):
28 while self
._count
== 0: # Multiple tasks may be waiting for
29 await self
._event
.wait() # a release
31 # When we yield, another task may succeed. In this case
32 await asyncio
.sleep(0) # the loop repeats
39 class BoundedSemaphore(Semaphore
):
40 def __init__(self
, value
=1):
41 super().__init
__(value
)
42 self
._initial
_value
= value
45 if self
._count
< self
._initial
_value
:
48 raise ValueError('Semaphore released more than acquired')