49 questions
2 votes
0 answers
174 views
Unexpected Memory Leak in Python's asyncio When Using contextvars with Nested TaskGroups
I'm encountering a subtle memory leak in a Python 3.12 application using asyncio, specifically when combining contextvars with deeply nested TaskGroup structures. The issue only appears under high ...
4 votes
2 answers
604 views
Keep context vars values between FastAPI/starlette middlewares depending on the middleware order
I am developing a FastAPI app, and my goal is to record some information in a Request scope and then reuse this information later in log records. My idea was to use context vars to store the "...
1 vote
2 answers
204 views
Asyncio: pass context or contextvar to add_done_callback
I am learning asyncio callbacks. My task is- I have a message dict, message codes are keys, message texts are values. In coro main I have to create a number of asynchronous tasks (in my case 3 tasks), ...
1 vote
2 answers
162 views
setting thread safe dictionary of runtime vars using contextvars
Basic process explanation: A process has some initialization parameters that have to be set dynamically at runtime. For example data for a certain amount of dates (final_lookback) have to be pulled ...
1 vote
1 answer
205 views
What happens with ContextVar if don't reset it?
With code below what will happen to SqlAlchemy session that is set in ContextVar if not to reset it? from contextvars import ContextVar from fastapi import FastAPI, BackgroundTasks from sqlalchemy ...
3 votes
2 answers
2k views
ContextVar set and reset in the same function fails - created in a different context
I have this function: async_session = contextvars.ContextVar("async_session") async def get_async_session() -> AsyncGenerator[AsyncSession, None]: async with async_session_maker() as ...
1 vote
1 answer
195 views
Intermittently losing ContextVar when passing from parent to child thread
I have a subclass of Thread that I use across my project. In this class, I pass in the ContextVar manually. However, at times (once or twice a day), I notice that the ContextVar in the child thread is ...
0 votes
2 answers
2k views
async version of Context.run for context vars in python asyncio?
Python has the contextvars.copy_context() function which lets you store a copy of the current values of all the ContextVars that are currently active. Then later, you can run with all those values ...
1 vote
1 answer
241 views
ContextVar MemoryLeak
The following code has a memory leak and I don't understand why there are references to MyObj. run(1) and run(2) are finished, the context is cleared. import asyncio import gc from contextvars import ...
2 votes
1 answer
375 views
Problem to pass contextvars to logging.handlers.QueueListener in Python
I'm trying to parallelize logs with Queuehandler and Queuelistener in a fastapi application but the contextvars setted on the main thread does not propagate to "_monitor" thread of the ...
1 vote
3 answers
107 views
*Dynamically* decorate a recursive function in Python
I have a scenario where I need to dynamically decorate recursive calls within a function in Python. The key requirement is to achieve this dynamically without modifying the function in the current ...
1 vote
1 answer
764 views
Context Variables should be created at the top module level and never in closures
In the documentation for the Python standard library module contextvars, it is stated that: Context Variables should be created at the top module level and never within closures. However, I am ...
0 votes
1 answer
219 views
proper use of contextvars with asyncio.create_server
The asyncio.create_server listens on the specified address:port and calls the methods of the supplied callable that implements BaseProtocol. Everything seems fine with the examples provided like this: ...
-2 votes
1 answer
376 views
Attribut error when using contextvar in python 3.10
When running this code below I got an AttributError: __enter__ I looked around with no success. import contextvars # Création d'une variable de contexte user_context = contextvars.ContextVar("...
1 vote
1 answer
748 views
understanding ContextVar object in Python
Here is a simple echo server that works correctly as expected - by "correctly" I mean, in the output we see that every user gets its own unique address when echoing inside ...