Skip to content

Instantly share code, notes, and snippets.

@jerryan999
Created September 29, 2021 10:26
Show Gist options
  • Save jerryan999/007fbcfeaa8241d445691c5736e42c29 to your computer and use it in GitHub Desktop.
Save jerryan999/007fbcfeaa8241d445691c5736e42c29 to your computer and use it in GitHub Desktop.
limit concurrency with Python asyncio
import asyncio
from random import randint
async def download(code):
wait_time = randint(1, 3)
print('downloading {} will take {} second(s)'.format(code, wait_time))
await asyncio.sleep(wait_time) # I/O, context will switch to main function
print('downloaded {}'.format(code))
sem = asyncio.Semaphore(3)
async def safe_download(i):
async with sem: # semaphore limits num of simultaneous downloads
return await download(i)
async def main():
tasks = [
asyncio.ensure_future(safe_download(i)) # creating task starts coroutine
for i
in range(9)
]
await asyncio.gather(*tasks) # await moment all downloads done
if __name__ == '__main__':
loop = asyncio.get_event_loop()
try:
loop.run_until_complete(main())
finally:
loop.run_until_complete(loop.shutdown_asyncgens())
loop.close()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment