]>
vault307.fbx.one Git - ir_remote.git/blob - primitives/barrier.py
2 # Copyright (c) 2018-2020 Peter Hinch
3 # Released under the MIT License (MIT) - see LICENSE file
5 # Now uses Event rather than polling.
8 import uasyncio
as asyncio
14 # A Barrier synchronises N coros. Each issues await barrier.
15 # Execution pauses until all other participant coros are waiting on it.
16 # At that point the callback is executed. Then the barrier is 'opened' and
17 # execution of all participants resumes.
20 def __init__(self
, participants
, func
=None, args
=()):
21 self
._participants
= participants
22 self
._count
= participants
26 self
._evt
= asyncio
.Event()
30 return # Other tasks have already reached barrier
31 await self
._evt
.wait() # Wait until last task reaches it
41 raise ValueError('Too many tasks accessing Barrier')
43 return False # At least 1 other task has not reached barrier
44 # All other tasks are waiting
45 if self
._func
is not None:
46 self
._res
= launch(self
._func
, self
._args
)
47 self
._count
= self
._participants
48 self
._evt
.set() # Release others
53 return self
._count
< self
._participants