Created
June 20, 2019 18:31
-
-
Save ralexstokes/b252271805de3ea73fdffd1c62630e41 to your computer and use it in GitHub Desktop.
basic python async
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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