50 questions
Advice
0
votes
4
replies
66
views
Unable to access ContextVar in sub function
I'm trying to access a variable value which is calculated at the beginning of the call stack. The value needs to be used deep down in a sub call, but the libraries being used don't allow any way to ...
2
votes
0
answers
193
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
670
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
216
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
176
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
215
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
206
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
246
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
390
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
109
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
784
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
223
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
391
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
782
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 ...
4
votes
1
answer
2k
views
Python: Pass ContexVars from parent thread to child thread spawn using threading.Thread()
I am setting some context variables using contextvars module that can be accessed across the modules running on the same thread.
Initially I was creating contextvars.ContextVars() object in each ...
1
vote
1
answer
3k
views
How can I use context vars in other file in python 3.7 or above?
I have a context var in in file a.py and I want to use it in b.py.
a.py:
import contextvars
cntx = contextvars.ContextVar("abcd")
b.py:
from .a import cntx
print(cntx.get())
Error:
...
0
votes
1
answer
369
views
Using a python package in the current app and its child package
I have a FastAPI app that uses package A as a dependency. On every request to the FastAPI app, package A stores some request string in a ContextVar inside the package. The FastAPI app also uses ...
1
vote
1
answer
584
views
Trouble with async context and structuring my code
I am using a library that requires async context(aioboto3).
My issue is that I can't call methods from outside the async with block on my custom S3StreamingFile instance. If I do so, python raises an ...
3
votes
1
answer
2k
views
python contextvars pass and access a variable along the chain of calls
Context variables are convenient when we need to pass a variable along the chain of calls so that they share the same context, in the case when this cannot be done through a global variable in the ...
4
votes
1
answer
3k
views
Why does getting a contextvar here not work?
So here's the basic code (sorry it's long)
import argparse
import asyncio
from contextvars import ContextVar
import sys
# This thing is the offender
message_var = ContextVar("message")
...
2
votes
1
answer
2k
views
How to mock (monkeypatch) a ContextVar in Pytest?
I am currently using asgi_correlation_id python package in my FastApi project.
This package exposes a ContextVar called correlation_id.
The usage is simple:
from asgi_correlation_id.context import ...
3
votes
2
answers
4k
views
Python: Copy context (contextvars.Context) to a separate thread
As for now, I've found a lot of examples on how contextvars module behaves with asyncio, but none on how one behaves with threads (asyncio.get_event_loop().run_in_executor, threading.Thread, and so on)...
1
vote
1
answer
2k
views
Python contextvars on dict
I need to make dictionary available as context var. I was trying to use @property setter, but don't understand how to correctly set key/value in this case.
I have websockets server and i need to make ...
0
votes
1
answer
495
views
Can not access contextVariables in decorator
I have python decorator and I need pass contextVariable inside my decorator or as argument request_id in function
Step 1: Declare contextVariables and methods
_correlation_id_ctx_var: ContextVar[str]...
2
votes
1
answer
4k
views
Using contextvar to keep track of async loop in Python
I am trying to work with a simple async example in Python, largely following this excellent answer here.
My goal is to set up a context variable and keep track of the series of calls by continuously ...
2
votes
1
answer
2k
views
How can i deal with context var in asyncio while it is in a 'while' cycle?
Contextvar 'value' doest change as it goes through 'while' cycle .
'''
import contextvars
import keyboard
import asyncio
import random
value = contextvars.ContextVar('value')
value.set('-')
async ...
1
vote
1
answer
729
views
contextvars: get and set atomic
In documentation I didn't find anything about contextvars update.
I need to have following atomically:
context_metadata = contextvars.ContextVar("context_logger_metadata")
my_dict = ...
16
votes
2
answers
6k
views
Is there any reason to use Python threading.local() rather than a ContextVar (in >= 3.7)
Python's thread-local data and ContextVars appear to achieve the same thing (while having slightly different APIs), with the only user-facing difference being that ContextVars work with async code (...
1
vote
4
answers
198
views
In python, Two functions calls some same workflows, how to distinguish the origin function at bottom?
I have two python functions A() and B(), they both call f1(), and f1() calls f2()... In f4(), I want to get the origin function name(A or B), Is there any smart way? I thought I can add a param for ...
2
votes
2
answers
2k
views
Copying contexvars.Context between tasks
I have a program (an ASGI server) that is structured roughly like this:
import asyncio
import contextvars
ctxvar = contextvars.ContextVar("ctx")
async def lifepsan():
ctxvar.set("...
2
votes
1
answer
606
views
Setting vars in a contextvars.Context directly
I want to call some function in a separate contextvars.Context, with some variables in this context set by the caller:
def call_with_context(var, callback, **cb_kw):
context = contextvars....
5
votes
1
answer
3k
views
Will a ContextVar leak memory in async logic if not reset after Exception?
If I have a structure in an async webserver like
import contextvars
...
my_context_var = contextvars.ContextVar("var")
@app.route("/foo") # decorator from webserver
async def ...
10
votes
3
answers
2k
views
Factory for contextvar default value
Setting a dictionary as ContextVar default:
var: ContextVar[dict] = ContextVar('var', default={})
...kinda works, as the dictionary will be available as default, but it always references the same ...
2
votes
2
answers
923
views
Access to contextvars in asyncio add_done_callback callback
In Python async function I'am creating ContextVar, task and attaching callback to it:
bbb = contextvars.ContextVar('aaa')
bbb.set(3)
task = self.loop.create_task(self.someFunc())
task....
10
votes
2
answers
4k
views
What happens if I don't reset Python's ContextVars?
Is this a memory leak in Python?
import contextvars
contextvar = contextvars.ContextVar('example')
while True:
string = 'hello world'
token = contextvar.set(string)
Is a contextvar a stack ...
0
votes
2
answers
164
views
Google CloudFunctions: ContextVar vs Global Var
Is there a difference between ContextVar and Global Var within Google Cloud Functions?
I noticed that as Google tries to re-use GCF instances some global vars classes are reused from one GCF ...
8
votes
1
answer
2k
views
How to run a coroutine inside a context?
In the Python docs about Context Vars a Context::run method is described to enable executing a callable inside a context so changes that the callable perform to the context are contained inside the ...
25
votes
1
answer
40k
views
Understanding Python contextvars
Regarding the following SO answer . I've made some changes in order to understand the difference between do use Contextvars and don't.
I expect at some point the variable myid gets corrupted but ...
3
votes
0
answers
165
views
Subclass python ContextVar
Is there a legal pythonic way to store one additional attribute in ContextVar?
I can't subclass ContextVar because of TypeError
I can
from contextvars import ContextVar as BaseContextVar
...
2
votes
0
answers
691
views
Are the concurrent tasks on the same Uvicorn worker using the same python context?
I have using FastAPI for a web api and I am using also python context vars. I know Uvicorn workers can fulfill many requests on multiple concurrent tasks.
Are those tasks are running in the same ...
1
vote
0
answers
404
views
Difference between using contextvars vs setting to Task.current_task()
As I see there are two options in asyncio to share a variable.
The first is to use contextvars and the second is to set a custom attribute to Task objects (feels like a hack).
My question is what is ...
2
votes
1
answer
7k
views
Sharing state between two async programs in python using asyncio and contextvars [duplicate]
I currently have two infinite asynchronous tasks running and want to share state between them. One task is a websocket connection that reads in messages then sends messages and the other reads in ...
5
votes
1
answer
656
views
Fetching Context Variables by identifier
I'm trying to use the new contextvars library (https://docs.python.org/3/library/contextvars.html) to make certain values available across modules in an async context, similar to ContextVars across ...
5
votes
1
answer
5k
views
Provide contextvars.Context with a ContextManager
I'm trying to manage transactions in my DB framework (I use MongoDB with umongo over pymongo).
To use transaction, one must pass a session kwarg along the whole call chain. I would like to provide a ...
0
votes
0
answers
502
views
How to translate commands without passing a context. (Discord Bot)
I'm coding a Discord bot. There you define your commands like
@commands.command()
async def hello(self, ctx):
await ctx.send("Hello world!")
This would create a command hello, which would send a ...
1
vote
3
answers
21k
views
No module named '_contextvars' in Python 3.7.3 virtual environment
I'm working on a Django project which requires Python3.7.3 virtual environment on Ubuntu 16.
So I created a virtual environment and installed all the requirements in it and verified it, activated it.
...
17
votes
4
answers
9k
views
ContextVars across modules
I am completely newb on asyncio and ContextVars, I just read up what's new in 3.7 and discovered ContextVars, I struggle to understand it's usage, all I know it's helpful in coroutines, instead of ...
7
votes
1
answer
2k
views
How do I write consistent stateful context managers?
EDIT: As pointed out by Thierry Lathuille, PEP567, where ContextVar was introduced, was not designed to address generators (unlike the withdrawn PEP550). Still, the main question remains. How do I ...