liveBook · Manning

Asyncio.run

As explained in Building LLM Applications with DSPy

asyncio.run is a function that runs an asynchronous entry-point coroutine in a new event loop.

asyncio.run(coro) creates an event loop and starts an async entry-point coroutine. It may be called more than once, but two event loops cannot run simultaneously. In Jupyter, an existing event loop can complicate its use; applying nest_asyncio is one way to allow nested loops. 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())

See asyncio.run start main(), which gathers two sleep-based coroutines and prints their results and elapsed time. section 7.5.1

How Building LLM Applications with DSPy covers it

How deep it goes
Explains loop creation, coroutine startup, repeat calls, and the restriction against simultaneous event loops.
Examples
Demonstrates two sleep-based coroutines gathered concurrently, returning both results and measuring elapsed time.
What you can do after
Operate asynchronous code by passing an async entry-point coroutine to asyncio.run().
How it connects
Connects coroutines, await, event loops, asyncio.gather, and nest_asyncio.

Also explained in

Compare how 2 books explain asyncio.run →

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.