Skip to content

Instantly share code, notes, and snippets.

@rgpower
Created February 17, 2021 15:29
Show Gist options
  • Select an option

  • Save rgpower/e6c5ed042bb9d332b89338b52285280a to your computer and use it in GitHub Desktop.

Select an option

Save rgpower/e6c5ed042bb9d332b89338b52285280a to your computer and use it in GitHub Desktop.
pass in a bunch of synchronous partials to get executed concurrently
"""
pass in a bunch of synchronous partials to get executed concurrently
example:
import asyncio
import functools
user_can_read = lambda res: functools.partial(
perms.can_read, some_user, some_res, some_perms,
)
is_readable = await async_gather(map(user_can_read, items)
"""
async def async_gather(synchronous_partials):
loop = asyncio.get_running_loop()
gathers = []
with concurrent.futures.ThreadPoolExecutor(max_workers=5) as pool:
for sp in synchronous_partials:
gathers.append(loop.run_in_executor(pool, sp))
return await asyncio.gather(*gathers)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment