Tanuki/tanuki.py

Support for Async

JackHopkins opened this issue · 0 comments

We should support asynchronous processing of patched function like as follows:

@monkey.patch
async def iterate_presidents() -> Iterable[str]:
    """"List the presidents of the United States""""


@monkey.patch
async def tell_me_more_about(topic: str) -> str:
   """Describe this in more detail"


# For each president listed, generate a description concurrently
start_time = time()
tasks = []
async for president in await iterate_presidents():
    # Use asyncio.create_task to schedule the coroutine for execution before awaiting it
    # This way descriptions will start being generated while the list of presidents is still being generated
    task = asyncio.create_task(tell_me_more_about(president))
    tasks.append(task)

descriptions = await asyncio.gather(*tasks)

Note: This was inspired by a Magentic example.