Created
March 21, 2019 12:18
-
-
Save Nydhal/478b2027a385e7a9b7402a9e77b680f8 to your computer and use it in GitHub Desktop.
SleepFizzBuzz using asyncio. https://twitter.com/tdhopper/status/1108387299639902216
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 FizzBuzz(i: int): | |
| await asyncio.sleep(i) | |
| print("Fizz"*(i%3==0)+"Buzz"*(i%5==0) or i) | |
| async def SleepFizzBuzz(l,loop): | |
| tasks = [loop.create_task(FizzBuzz(i)) for i in range(1,l+1)] | |
| for t in tasks: | |
| await t | |
| async def main(loop): | |
| await SleepFizzBuzz(30,loop) | |
| if __name__ == '__main__': | |
| loop = asyncio.get_event_loop() | |
| loop.run_until_complete(main(loop)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment