Skip to content

Instantly share code, notes, and snippets.

@jiewmeng
Created June 9, 2022 03:23
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 jiewmeng/fb3368119cf536fd1bc79e92375456d1 to your computer and use it in GitHub Desktop.
Save jiewmeng/fb3368119cf536fd1bc79e92375456d1 to your computer and use it in GitHub Desktop.
Python asyncio
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())
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