Skip to content

Instantly share code, notes, and snippets.

@agronholm
Created February 4, 2021 14:54
Show Gist options
  • Save agronholm/2e8ba9f772a6dcf6170d956a8a95510c to your computer and use it in GitHub Desktop.
Save agronholm/2e8ba9f772a6dcf6170d956a8a95510c to your computer and use it in GitHub Desktop.
from concurrent.futures import as_completed
from anyio import start_blocking_portal, sleep
async def long_running_task(index):
await sleep(1)
print(f'Task {index} running...')
await sleep(index)
return f'Task {index} return value'
with start_blocking_portal() as portal:
futures = [portal.spawn_task(long_running_task, i) for i in range(1, 5)]
for future in as_completed(futures):
print(future.result())
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment