Created
June 9, 2022 03:23
-
-
Save jiewmeng/fb3368119cf536fd1bc79e92375456d1 to your computer and use it in GitHub Desktop.
Python asyncio
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 | |
from random import randint | |
async def apiCallWorker(name, queue): | |
while True: | |
apiId = await queue.get() | |
sleepTimeInSec = randint(0, 3) | |
print(f"worker {name} apiId {apiId} sleeping for {sleepTimeInSec}") | |
await asyncio.sleep(sleepTimeInSec) | |
print(f"worker {name} apiId {apiId} done") | |
queue.task_done() | |
async def main(): | |
queue = asyncio.Queue() | |
# put work on queue | |
for id in range(10): | |
queue.put_nowait(id) | |
# create 3 workers | |
pool = [] | |
for i in range(3): | |
worker = asyncio.create_task(apiCallWorker(f"worker-{i}", queue)) | |
pool.append(worker) | |
await queue.join() | |
for worker in pool: | |
worker.cancel() | |
await asyncio.gather(*pool, return_exceptions=True) | |
print("All done!") | |
asyncio.run(main()) |
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 | |
from random import randint | |
async def doApiCall(id: str): | |
sleepTimeInSec = randint(0, 3) | |
print(f"[{id}] sleeping for {sleepTimeInSec}s") | |
await asyncio.sleep(sleepTimeInSec) | |
print(f"[{id}] done") | |
async def main(): | |
await asyncio.gather(*[ doApiCall(id) for id in range(0, 10) ]) | |
print("All done!") | |
asyncio.run(main()) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment