Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

aiocontextvars ContextVar value not kept independently for the different async functions that set it #143

Closed
ocelotl opened this issue Jan 17, 2020 · 2 comments

Comments

@ocelotl
Copy link

ocelotl commented Jan 17, 2020

  • aiocontextvars version: 0.2.2
  • Python version: 3.5.0
  • Operating System: Ubuntu 18.04.3

Description

Mismatching results when running an asyncio script with Python 3.5.0 and aiocontextvars vs. Python 3.7.5rc1 and native contextvars package.

What I Did

I created a virtual environment with Python 3.5.0 and installed aiocontextvars via pip3. I activated the virtual environment and ran the script below. I ran the same script with Python 3.7.5rc1 and got different results. The results that I got from running with Python 3.7.5rc1 are the ones I expected. When I run the script with aiocontextvars it seems like the ContextVar value is not kept independently for the different async functions that set its value.

(3-5-0) ocelotl@harrison:~$ cat async_contextvar.py 
from asyncio import sleep, gather, get_event_loop
from contextvars import ContextVar

local_name = ContextVar("async_name")


async def waiting(async_name, first_sleep, second_sleep):

    await sleep(first_sleep)

    local_name.set(async_name)

    print("async_name:\t{}".format(async_name))
    print("local_name:\t{}".format(local_name.get()))
    print()

    await sleep(second_sleep)

    print("async_name:\t{}".format(async_name))
    print("local_name:\t{}".format(local_name.get()))
    print()


async def main():
    await gather(waiting("A", 0, 1), waiting("B", 0.1, 3), waiting("C", 2, 1))

loop = get_event_loop()
loop.run_until_complete(main())
loop.close()
(3-5-0) ocelotl@harrison:~$ lsb_release -a
No LSB modules are available.
Distributor ID: Ubuntu
Description:    Ubuntu 18.04.3 LTS
Release:        18.04
Codename:       bionic
(3-5-0) ocelotl@harrison:~$ python3 --version
Python 3.5.0
(3-5-0) ocelotl@harrison:~$ pip3 freeze
aiocontextvars==0.2.2
contextvars==2.4
immutables==0.11
You are using pip version 7.1.2, however version 19.3.1 is available.
You should consider upgrading via the 'pip install --upgrade pip' command.
(3-5-0) ocelotl@harrison:~$ python3 async_contextvar.py 
async_name:     A
local_name:     A

async_name:     B
local_name:     B

async_name:     A
local_name:     B

async_name:     C
local_name:     C

async_name:     C
local_name:     C

async_name:     B
local_name:     C

(3-5-0) ocelotl@harrison:~$ 
ocelotl@harrison:~$ lsb_release -a
No LSB modules are available.
Distributor ID: Ubuntu
Description:    Ubuntu 18.04.3 LTS
Release:        18.04
Codename:       bionic
ocelotl@harrison:~$ python3 --version
Python 3.7.5rc1
ocelotl@harrison:~$ python3 async_contextvar.py 
async_name:     A
local_name:     A

async_name:     B
local_name:     B

async_name:     A
local_name:     A

async_name:     C
local_name:     C

async_name:     C
local_name:     C

async_name:     B
local_name:     B

ocelotl@harrison:~$ 
@condorcet
Copy link

@ocelotl Hi! Seems that you forget to import aiocontextvars at the beginning, it makes all magic around contextvars :(

@ocelotl
Copy link
Author

ocelotl commented Jan 17, 2020

@ocelotl Hi! Seems that you forget to import aiocontextvars at the beginning, it makes all magic around contextvars :(

Yes! That does it, @condorcet, thanks. I'm closing this issue right now. 👍

@ocelotl ocelotl closed this as completed Jan 17, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants