Skip to content

Instantly share code, notes, and snippets.

@devlights
Created November 21, 2018 08:54
Show Gist options
  • Save devlights/5f3cd837a9792cd1b397a4be5c334395 to your computer and use it in GitHub Desktop.
Save devlights/5f3cd837a9792cd1b397a4be5c334395 to your computer and use it in GitHub Desktop.
[python][asyncio] asyncio.waitを用いたサンプル
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