Skip to content

Instantly share code, notes, and snippets.

@ralexstokes
Created June 20, 2019 18:31
Show Gist options
  • Save ralexstokes/b252271805de3ea73fdffd1c62630e41 to your computer and use it in GitHub Desktop.
Save ralexstokes/b252271805de3ea73fdffd1c62630e41 to your computer and use it in GitHub Desktop.
basic python async
import asyncio
async def count(delay):
# this will block everything :(
# time.sleep(2)
# will explode the other stuff
# raise Exception("hi")
await asyncio.sleep(delay)
return 42
async def main():
results = await asyncio.gather(count(1), count(1), count(1))
print(results)
# regular sync fn
def foo(a):
print(a + 1)
print("hi")
print(a + 2)
if __name__ == "__main__":
import time
s = time.perf_counter()
asyncio.run(main())
elapsed = time.perf_counter() - s
print(f"{__file__} executed in {elapsed:0.2f} seconds.")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment