Created
November 21, 2018 08:54
-
-
Save devlights/5f3cd837a9792cd1b397a4be5c334395 to your computer and use it in GitHub Desktop.
[python][asyncio] asyncio.waitを用いたサンプル
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 factorial(id: int, number: int): | |
f = 1 | |
for i in range(2, number + 1): | |
print(f'[{id}]: compute {i} ...') | |
await asyncio.sleep(1) | |
f *= i | |
print(f'[{id}]: factorial {number} = {f}') | |
async def main(): | |
print('begin') | |
futures = [factorial(i, i) for i in range(2, 10)] | |
await asyncio.wait(futures) | |
print('end') | |
if __name__ == "__main__": | |
asyncio.run(main()) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment