Created
September 21, 2020 00:07
-
-
Save smartworld-dm/ea24ea862bb65fb43cc55b64749338b3 to your computer and use it in GitHub Desktop.
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 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