Last active
June 25, 2019 23:57
-
-
Save smoofra/cdc6cd5cab5dac6c7868082c2516c8f6 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
#!/usr/bin/python3 | |
import asyncio | |
async def doitem(n, sem): | |
try: | |
print('foo', n) | |
await asyncio.sleep(1) | |
print('bar', n) | |
finally: | |
sem.release() | |
async def run(): | |
sem = asyncio.Semaphore(5) | |
for n in range(40): | |
await sem.acquire() | |
asyncio.create_task(doitem(n, sem)) | |
loop = asyncio.get_event_loop() | |
task = loop.create_task(run()) | |
loop.run_until_complete(task) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment