Skip to content

Instantly share code, notes, and snippets.

@smoofra
Last active June 25, 2019 23:57
Show Gist options
  • Save smoofra/cdc6cd5cab5dac6c7868082c2516c8f6 to your computer and use it in GitHub Desktop.
Save smoofra/cdc6cd5cab5dac6c7868082c2516c8f6 to your computer and use it in GitHub Desktop.
#!/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