Created
April 11, 2022 18:15
-
-
Save harryscholes/6bf5d52a6f9e2c573d9a487afb05f238 to your computer and use it in GitHub Desktop.
Python asyncio with semaphore
This file contains 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 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