Home / Your Stack / async / await
Lesson 2 of 4

async / await

Concept

Regular functions block — nothing else runs until they finish. async functions can pause while waiting (for a database, a network call) and let other requests proceed. await marks the pause points.
m3shdup/app/db.py
# SYNC (blocking) — everything waits
def get_agents_sync():
    conn = sqlite3.connect('data/mesh.db')  # blocks
    result = conn.execute('SELECT * FROM agents')  # blocks
    return result.fetchall()

# ASYNC (non-blocking) — other requests can proceed
async def get_agents():
    conn = await aiosqlite.connect('data/mesh.db')  # pauses, doesn't block
    result = await conn.execute('SELECT * FROM agents')  # pauses, doesn't block
    return await result.fetchall()

What's happening

Your mesh handles many simultaneous requests — agents heartbeating, tasks dispatching, health checks. Without async, request #2 waits for request #1's database query to finish. With async, while request #1 waits for the DB, request #2 can start processing.

Quick Check — 3 questions

1. What does await do?

2. Can you use await inside a regular (non-async) function?

3. Why does your mesh code use async?