Skip to content

Instantly share code, notes, and snippets.

@davidfarrugia
Created October 31, 2021 17:30
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save davidfarrugia/f43c5978bd14bba4aa429547243afa54 to your computer and use it in GitHub Desktop.
Save davidfarrugia/f43c5978bd14bba4aa429547243afa54 to your computer and use it in GitHub Desktop.
A simple introduction to asyncio in Python
import asyncio
# define our coroutine
async def say_hello():
print('Hello')
# asynchronous sleep of 1 second
await asyncio.sleep(1)
print('World')
# we initialise our event loop
loop = asyncio.get_event_loop()
# we run our coroutine in the event loop until it is completed
loop.run_until_complete(say_hello())
# close the event loop
loop.close()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment