Last active
August 13, 2019 18:17
-
-
Save djosix/58d260d3bcb5beddebe287aa964aef00 to your computer and use it in GitHub Desktop.
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
import functools | |
import asyncio | |
import boto3 | |
class AsyncBoto3Client: | |
def __init__(self, *args, **kwargs): | |
client = boto3.client(*args, **kwargs) | |
self.__client = client | |
for name in dir(client): | |
if name.startswith('_'): | |
continue | |
func = getattr(client, name) | |
if not callable(func): | |
continue | |
def wrapper(func, *args, **kwargs): | |
return asyncio.get_running_loop().run_in_executor( | |
None, lambda: func(*args, **kwargs)) | |
setattr(self, name, functools.partial(wrapper, func)) | |
async def main(): | |
ecs = AsyncBoto3Client('ecs') | |
def wrap_task(task): | |
async def coro(task): | |
print(await task) | |
return asyncio.Task(coro(task)) | |
future = asyncio.gather( | |
wrap_task(ecs.list_clusters()), | |
wrap_task(ecs.list_clusters()), | |
wrap_task(ecs.list_clusters()), | |
wrap_task(ecs.list_clusters()), | |
wrap_task(ecs.list_clusters()), | |
wrap_task(ecs.list_clusters()), | |
wrap_task(ecs.list_clusters()), | |
) | |
print('feels so good') | |
await future | |
asyncio.run(main()) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
very dirty solution