async def run_cancel_test2(): barrier = Barrier(3) loop = asyncio.get_event_loop() loop.create_task(NamedTask('rats_1', rats, 1, barrier=barrier)()) loop.create_task(NamedTask('rats_2', rats, 2, barrier=barrier)()) print('Running two tasks') await asyncio.sleep(10) print('About to cancel tasks') NamedTask.cancel('rats_1') # These will stop when their wait is complete NamedTask.cancel('rats_2') await barrier # So wait for that to occur. print('tasks were cancelled')
async def run_cancel_test3(): barrier = Barrier(2) loop = asyncio.get_event_loop() loop.create_task(NamedTask('cant3', cant3, barrier=barrier)()) print('Task cant3 running status', NamedTask.is_running('cant3')) await asyncio.sleep(3) print('Task cant3 running status', NamedTask.is_running('cant3')) print('About to cancel task') NamedTask.cancel('cant3') print('Cancelled') print('Task cant3 running status', NamedTask.is_running('cant3')) await barrier print('tasks were cancelled')
def barrier_test(): printexp('''0 0 0 Synch 1 1 1 Synch 2 2 2 Synch 3 3 3 Synch 4 4 4 Synch ''') barrier = Barrier(3, callback, ('Synch', )) loop = asyncio.get_event_loop() for _ in range(3): loop.create_task(report(barrier)) loop.run_until_complete(killer(2)) loop.close()
async def run_sema_test(bounded): num_coros = 5 loop = asyncio.get_event_loop() barrier = Barrier(num_coros + 1) if bounded: semaphore = BoundedSemaphore(3) else: semaphore = Semaphore(3) for n in range(num_coros): loop.create_task(run_sema(n, semaphore, barrier)) await barrier # Quit when all coros complete try: semaphore.release() except ValueError: print('Bounded semaphore exception test OK')
loop = asyncio.get_event_loop() loop.run_until_complete(run_event_test()) # ************ Barrier test ************ async def killer(duration): await asyncio.sleep(duration) def callback(text): print(text) barrier = Barrier(3, callback, ('Synch', )) async def report(): for i in range(5): print('{} '.format(i), end='') await barrier def barrier_test(): loop = asyncio.get_event_loop() for _ in range(3): loop.create_task(report()) loop.run_until_complete(killer(2)) loop.close()