Skip to content
Discussion options

You must be logged in to vote

It uses something called contextvars under the hood.

Imagine it like a tree - if you create a transaction then any branches lower down (i.e. function calls) have access to it.

Here's an example where we run 3 async functions, and each has its own transaction (the transactions don't interfere with each other):

import asyncio
import random

from piccolo.engine.postgres import PostgresEngine


DB = PostgresEngine({"database": "piccolo"})


async def which_transaction(name: str):
    # Sleep to simulate doing stuff:
    await asyncio.sleep(random.random())
    print(f"{name}=", id(DB.current_transaction.get()))


async def do_stuff(name: str):
    await which_transaction(name=name)


async def 

Replies: 1 comment

Comment options

You must be logged in to vote
0 replies
Answer selected by dantownsend
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants