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 using thread.local
should use ContextVars
. But none of the official doc and top google search result could help me truely understand its purpose.
So is convextvars shared across modules? I tried:
example.py
from contextvars import ContextVar
number = ContextVar('number', default=100)
number.set(1)
then I try to import number.py
(playground) Jamess-MacBook-Pro-2:playground jlin$ python3.7
Python 3.7.0 (v3.7.0:1bf9cc5093, Jun 26 2018, 23:26:24)
[Clang 6.0 (clang-600.0.57)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import example
>>> from contextvars import ContextVar
>>> number = ContextVar('number', default=200)
>>> number.get()
200
I was expecting number.get()
would return 1, but obviously I have understood its purpose wrong.
Could someone please help me understand this?
from ContextVars across modules
No comments:
Post a Comment