Skip to content

Critique request: Implementing Litestar 2.0 with other concurrent processes sharing access to a data repository object #2713

Answered by provinzkraut
maovidal asked this question in Q&A
Discussion options

You must be logged in to vote

Here are some thoughts and questions:

The app is a backend running 3 concurrent processes

I don't see anything running a dedicated process, just asyncio tasks. Are those what you are talking about here?

Code like this:

asyncio.create_task(client_a.loop())

is dangerous; If you don't save a reference to your tasks, they might be garbage collected at some point. I recommend using a TaskGroup to handle those instead.

Your setup code can also be simplified somewhat:

@asynccontextmanager
async def manage_client_lifespan(app: Litestar) -> AsyncGenerator[None, None]:
    repo_a = getattr(app.state, "repo_a", None)
    if repo_a is None:
        repo_a = app.state.repo_a= Repo("A")

    repo_b = g…

Replies: 1 comment 1 reply

Comment options

You must be logged in to vote
1 reply
@maovidal
Comment options

Answer selected by JacobCoffee
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