Differences between revisions 3 and 4
Revision 3 as of 2018-07-08 20:28:40
Size: 641
Editor: PieterSmit
Comment:
Revision 4 as of 2019-08-06 04:36:41
Size: 1274
Editor: PieterSmit
Comment:
Deletions are marked like this. Additions are marked like this.
Line 17: Line 17:
 == asyncio sleep function that can be quit with event ==
 * globals.exitEvent = asyncio.Event()
{{{
async def sleep(timeout):
    '''
    replacement asyncio.sleep function.
    it also exits when receiving globals.exitEvent event.
    '''
    print(f"sleep({timeout}) enter ... exitEvent={globals.exitEvent.is_set()}")
    try:
        await asyncio.wait_for( globals.exitEvent.wait(), timeout )
        print("sleep: got globals.exitEvent() quitting ...")
    except asyncio.TimeoutError:
        print(f"sleep({timeout}) except asyncio.TimeoutError - pass")
        pass
    return globals.exitEvent.is_set(
Line 18: Line 34:
}}}

Python Asyncio

Summary

  • loop = asyncio.get_event_loop()
  • loop.run_forever() - Run until stop() is called, Blocks
  • loop.close() Close the event loop. The loop must not be running. Pending callbacks will be lost.
  • loop.call_soon(callback, *args)
  • loop.call_later(delay, callback, *args) - delay seconds
  • asyncio.time() - Return the current time, as a float value
  • loop.create_task(coro) - run async function as co-routine. == asyncio sleep function that can be quit with event ==
  • globals.exitEvent = asyncio.Event()

async def sleep(timeout):
    '''
    replacement asyncio.sleep function.
    it also exits when receiving globals.exitEvent event.
    '''
    print(f"sleep({timeout}) enter ... exitEvent={globals.exitEvent.is_set()}")
    try:
        await asyncio.wait_for( globals.exitEvent.wait(), timeout )
        print("sleep: got globals.exitEvent() quitting ...")
    except asyncio.TimeoutError:
        print(f"sleep({timeout}) except asyncio.TimeoutError - pass")
        pass
    return globals.exitEvent.is_set(

...

Python/AsyncIo (last edited 2019-08-06 04:36:41 by PieterSmit)