Created
February 17, 2021 15:29
-
-
Save rgpower/e6c5ed042bb9d332b89338b52285280a to your computer and use it in GitHub Desktop.
pass in a bunch of synchronous partials to get executed concurrently
This file contains hidden or 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
| """ | |
| 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