Skip to content

Instantly share code, notes, and snippets.

@smartworld-dm
Created September 21, 2020 00:07
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save smartworld-dm/ea24ea862bb65fb43cc55b64749338b3 to your computer and use it in GitHub Desktop.
Save smartworld-dm/ea24ea862bb65fb43cc55b64749338b3 to your computer and use it in GitHub Desktop.
import asyncio
async def worker(name, number_of_hours):
for i in range(number_of_hours):
await asyncio.sleep(1)
print(f"@20 - Worker {name} is working {i + 1} hours...")
async def workers():
# Schedule three calls *concurrently*:
print('@10 - 3 workers will start working')
await asyncio.gather(
worker("A", 4),
worker("B", 6),
worker("C", 8),
)
print("@11 - 3 workers finished all")
async def main():
# Wait for at most 1 second
try:
await asyncio.wait_for(workers(), timeout=5)
except asyncio.TimeoutError:
print('Timeout! stop working! :)')
loop = asyncio.get_event_loop()
result = loop.run_until_complete(main())
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment