Skip to main content
Filter by
Sorted by
Tagged with
Advice
0 votes
4 replies
66 views

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 ...
orange's user avatar
  • 8,252
2 votes
0 answers
193 views

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 ...
user avatar
4 votes
2 answers
670 views

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 "...
Christian's user avatar
  • 313
1 vote
2 answers
216 views

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), ...
IzaeDA's user avatar
  • 439
1 vote
2 answers
176 views

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 ...
shoshanie's user avatar
1 vote
1 answer
215 views

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 ...
Альберт Александров's user avatar
3 votes
2 answers
2k views

I have this function: async_session = contextvars.ContextVar("async_session") async def get_async_session() -> AsyncGenerator[AsyncSession, None]: async with async_session_maker() as ...
Tom's user avatar
  • 8,181
1 vote
1 answer
206 views

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 ...
Kyuubi's user avatar
  • 1,240
0 votes
2 answers
2k views

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 ...
Joseph Garvin's user avatar
1 vote
1 answer
246 views

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 ...
mq7's user avatar
  • 1,329
2 votes
1 answer
390 views

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 ...
Maurício Silva's user avatar
1 vote
3 answers
109 views

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 ...
Michiel Karrenbelt's user avatar
1 vote
1 answer
784 views

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 ...
Fang Hung-chien's user avatar
0 votes
1 answer
223 views

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: ...
rugubara's user avatar
-2 votes
1 answer
391 views

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("...
Herve Meftah's user avatar
  • 1,085
1 vote
1 answer
782 views

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 ...
S.B's user avatar
  • 17.1k
4 votes
1 answer
2k views

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 ...
Abhay's user avatar
  • 536
1 vote
1 answer
3k views

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: ...
Aravindh's user avatar
0 votes
1 answer
369 views

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 ...
Amr Saeed's user avatar
  • 305
1 vote
1 answer
584 views

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 ...
Dani's user avatar
  • 953
3 votes
1 answer
2k views

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 ...
A l w a y s S u n n y's user avatar
4 votes
1 answer
3k views

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") ...
Brent Parker's user avatar
2 votes
1 answer
2k views

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 ...
raghavsikaria's user avatar
3 votes
2 answers
4k views

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)...
Vlad Vladovich's user avatar
1 vote
1 answer
2k views

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 ...
Digital God's user avatar
0 votes
1 answer
495 views

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]...
Dmitriy_kzn's user avatar
2 votes
1 answer
4k views

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 ...
lazarea's user avatar
  • 1,369
2 votes
1 answer
2k views

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 ...
wasjusty's user avatar
1 vote
1 answer
729 views

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 = ...
Rudziankoŭ's user avatar
  • 11.3k
16 votes
2 answers
6k views

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 (...
Graham Lea's user avatar
  • 6,493
1 vote
4 answers
198 views

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 ...
Q.Tong's user avatar
  • 21
2 votes
2 answers
2k views

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("...
LoveToCode's user avatar
2 votes
1 answer
606 views

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....
wRAR's user avatar
  • 25.7k
5 votes
1 answer
3k views

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 ...
ti7's user avatar
  • 19.9k
10 votes
3 answers
2k views

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 ...
Tuukka Mustonen's user avatar
2 votes
2 answers
923 views

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....
ljweko's user avatar
  • 35
10 votes
2 answers
4k views

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 ...
Rol's user avatar
  • 729
0 votes
2 answers
164 views

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 ...
syldman's user avatar
  • 795
8 votes
1 answer
2k views

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 ...
Rodrigo Oliveira's user avatar
25 votes
1 answer
40k views

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 ...
Ricardo's user avatar
  • 8,391
3 votes
0 answers
165 views

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 ...
Belegnar's user avatar
  • 917
2 votes
0 answers
691 views

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 ...
Assem's user avatar
  • 12.2k
1 vote
0 answers
404 views

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 ...
Alex Oleshkevich's user avatar
2 votes
1 answer
7k views

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 ...
avenmia's user avatar
  • 2,665
5 votes
1 answer
656 views

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 ...
vogelier's user avatar
5 votes
1 answer
5k views

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 ...
Jérôme's user avatar
  • 14.9k
0 votes
0 answers
502 views

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 ...
Spinne's user avatar
  • 55
1 vote
3 answers
21k views

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. ...
Underoos's user avatar
  • 5,276
17 votes
4 answers
9k views

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 ...
James Lin's user avatar
  • 26.8k
7 votes
1 answer
2k views

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 ...
javidcf's user avatar
  • 59.9k