Skip to content

Instantly share code, notes, and snippets.

@harryscholes
Created April 11, 2022 18:15
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 harryscholes/6bf5d52a6f9e2c573d9a487afb05f238 to your computer and use it in GitHub Desktop.
Save harryscholes/6bf5d52a6f9e2c573d9a487afb05f238 to your computer and use it in GitHub Desktop.
Python asyncio with semaphore
import asyncio
async def foo(sem, i):
async with sem:
print("acquired", i)
await asyncio.sleep(1)
print("released", i)
async def main():
sem = asyncio.Semaphore(2)
await asyncio.gather(*[foo(sem, i) for i in range(4)])
asyncio.run(main())
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment