Last active
April 17, 2024 12:59
-
-
Save ftnext/9d2e552fedf6dc0db5639fccbaa6d2a7 to your computer and use it in GitHub Desktop.
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 single_request(i: int) -> int: | |
print("start", i) | |
await asyncio.sleep(i) | |
print("end", i) | |
return i | |
async def main(): | |
semaphore = asyncio.Semaphore(2) | |
async def coroutine(i: int) -> int: | |
async with semaphore: | |
return await single_request(i) | |
return await asyncio.gather(*[coroutine(i) for i in range(5, 0, -1)]) | |
if __name__ == "__main__": | |
print(asyncio.run(main())) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment