liveBook · Manning

Asynchronous operation

As explained in Building LLM Applications with DSPy

Asynchronous execution lets a task proceed without blocking while it waits for another operation to complete.

Asynchronous execution lets coroutine tasks make progress while other operations are waiting. Python’s asyncio event loop schedules tasks so independent waits can overlap instead of simply adding their durations. section 7.5.1

Overlapping waits

With asyncio.gather(), two coroutines that sleep for five and three seconds complete in about five seconds; their returned values remain in input order. asyncio.create_task() is another way to schedule tasks. section 7.5.1

Listing 7.17 asyncio example
import asyncio
import time

async def func1():
    print("Start func1")
    await asyncio.sleep(5)
    print("End func1")
    return "func1 complete"

async def func2():
    print("Start func2")
    await asyncio.sleep(3)
    print("End func2")
    return "func2 complete"

async def main():
    start_time = time.time()
    co_routines = asyncio.gather(func1(), func2())
    a, b = await co_routines
    print(a, b)
    end_time = time.time()
    print("time", end_time-start_time)

asyncio.run(main())

Runnable asyncio example measuring concurrent coroutine waits and collecting their results. section 7.5.1

How Building LLM Applications with DSPy covers it

How deep it goes
Explains awaitable coroutines, event-loop task scheduling, gather(), create_task(), ordered results, and execution timing.
Examples
Demonstrates two sleeping coroutines whose five- and three-second waits finish in about five seconds, returning results in input order.
What you can do after
Apply it via asyncio.gather() or create_task() to run coroutine tasks concurrently and time whether their waits overlap.
How it connects
Connects coroutines and the event loop with asyncio, and links DSPy modules to asynchronous aforward() support.

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Building LLM Applications with DSPy. The section links point to the parts of the book it draws on.