Questions tagged [python-contextvars]

Contextvars are a concept similar to thread-local variables, intended to provide value isolation across co-routines executed in parallel in a Python async application. They are implemented in a module with the same name introduced in the stdlib in Python 3.7.0, specified in PEP-567.

Python-contextvars were first justified and introduced in PEP-550, which ended-up being rejected mainly for holding a too-ambitious scope. A narrowed down proposal came in PEP-567 that ended up implemented in Python 3.7.0. The idea is to be able to have cross-local scope values that can be shared across co-routines running in the same task under async-io, in the same way that thread-local-storage variables can provide value isolation for variable-names across different threads.

PEP 550 presents context vars as:

This PEP adds a new generic mechanism of ensuring consistent access to non-local state in the context of out-of-order execution, such as in Python generators and coroutines.

Thread-local storage, such as threading.local(), is inadequate for programs that execute concurrently in the same OS thread. This PEP proposes a solution to this problem.

34 questions
2
votes
1 answer

Sharing state between two async programs in python using asyncio and contextvars

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 incoming light data. I want to send a boolean between…
avenmia
  • 2,015
  • 4
  • 25
  • 49
1
vote
1 answer

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 python file hoping that there is only single context…
Abhay
  • 524
  • 1
  • 13
  • 28
1
vote
1 answer

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: Traceback (most recent call last): File…
1
vote
1 answer

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 exception, telling me that HttpClient is None. I…
Dani
  • 824
  • 7
  • 12
1
vote
1 answer

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 case of concurrency. Context variables can be used…
A l w a y s S u n n y
  • 36,497
  • 8
  • 60
  • 103
1
vote
1 answer

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 correlation_id id = correlation_id.get() Now, it…
raghavsikaria
  • 867
  • 17
  • 30
1
vote
1 answer

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 appending to it. I know that context variables can…
lazarea
  • 1,129
  • 14
  • 43
1
vote
1 answer

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 = context_metadata.get() my_dict['appended'] =…
Rudziankoŭ
  • 10,681
  • 20
  • 92
  • 192
1
vote
4 answers

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 each functions, but it makes my codes quite ugly.…
Q.Tong
  • 21
  • 3
1
vote
0 answers

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 context ? If I set a context var in one task, It…
Assem
  • 11,574
  • 5
  • 59
  • 97
1
vote
0 answers

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 the difference between these two options? Are…
Alex Oleshkevich
  • 184
  • 2
  • 12
1
vote
3 answers

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. But when I try to run the Django server using…
Underoos
  • 4,708
  • 8
  • 42
  • 85
0
votes
1 answer

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 package B. This package B uses package A as an internal…
0
votes
1 answer

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") class ServerProtocol(asyncio.Protocol): def…
Brent Parker
  • 709
  • 1
  • 5
  • 19
0
votes
1 answer

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 dictionary variable be unique for each client. Of…
Digital God
  • 471
  • 1
  • 5
  • 17