Python coroutines

From wikinotes

Python coroutines introduce eventloops to python's standard library (the reactor pattern).

  • an eventloop is created
  • a coroutine is added to the eventloop


async def count(seconds):
    await asyncio.sleep(seconds)
    return True


async def main():
    my_coroutine = count()
    result = await my_coroutine
    assert result is True


if __name__ == '__main__':
    asyncio.run(main())


async def <fn>      # function is a coroutine
await <coroutine>   # wait for coroutine to complete, and return result


A very good tutorial: https://realpython.com/async-io-python/