Skip to content

Instantly share code, notes, and snippets.

@smoofra
Last active August 17, 2019 00:34
Show Gist options
  • Save smoofra/051f3e96f32a25a66fef4dc59fe81163 to your computer and use it in GitHub Desktop.
Save smoofra/051f3e96f32a25a66fef4dc59fe81163 to your computer and use it in GitHub Desktop.
semtest2.py
#!/usr/bin/python3
# In this example have 50 jobs to do concurrently, but only 10 are allowed to be
# done at a time. We use a semaphore to limit spawning the next task untill one
# of the 10 currently runninng ones finishes. At the end we gather up the results.
import asyncio
import os
import sys
class TaskLimitSemaphore(asyncio.Semaphore):
async def create_task(self, awaitable):
async def wrapper():
try:
return await awaitable
finally:
self.release()
await self.acquire()
return asyncio.create_task(wrapper())
sem = TaskLimitSemaphore(3)
async def do_one(g):
result = 1 / (g - 9)
print(g, end=' ')
sys.stdout.flush()
await asyncio.sleep(.2)
return result
async def do_all():
tasks = list()
for g in range(20):
task = await sem.create_task(do_one(g))
tasks.append(task)
# If we leave off return_exceptions, it will just re-raise them here.
results = await asyncio.gather(*tasks, return_exceptions=True)
print()
for g,result in enumerate(results):
print(f'1 / ({g} - 9) = {result}')
loop = asyncio.get_event_loop()
task = loop.create_task(do_all())
loop.run_until_complete(task)
0 1 2 3 4 5 6 7 8 10 11 12 13 14 15 16 17 18 19
1 / (0 - 9) = -0.1111111111111111
1 / (1 - 9) = -0.125
1 / (2 - 9) = -0.14285714285714285
1 / (3 - 9) = -0.16666666666666666
1 / (4 - 9) = -0.2
1 / (5 - 9) = -0.25
1 / (6 - 9) = -0.3333333333333333
1 / (7 - 9) = -0.5
1 / (8 - 9) = -1.0
1 / (9 - 9) = division by zero
1 / (10 - 9) = 1.0
1 / (11 - 9) = 0.5
1 / (12 - 9) = 0.3333333333333333
1 / (13 - 9) = 0.25
1 / (14 - 9) = 0.2
1 / (15 - 9) = 0.16666666666666666
1 / (16 - 9) = 0.14285714285714285
1 / (17 - 9) = 0.125
1 / (18 - 9) = 0.1111111111111111
1 / (19 - 9) = 0.1
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment