Python - python3.7 new contextvars vs Thread local (threading.local)

to sum up

And threading.local () is similar. Python3.7 added.
thread.local (): different threads, hold different values for the same variable.
contextvars: different contexts, to save different values for the same variable. For example: when the same thread, or asynchronously coroutine different concurrent tasks (e.g. ASYNCIO) a variable of the same applies hereinafter have different values.

reference

https://realpython.com/python37-new-features/#context-variables
https://www.dongwm.com/post/137/
https://www.pythoninsight.com/2019/03/context-variables/
https://valarmorghulis.io/tech/201904-contextvars-and-thread-local/
https://stackoverflow.com/questions/56371373/contextvars-across-modules
https://stackoverflow.com/questions/50854974/context-variables-in-python
https://docs.python.org/3/library/contextvars.html
https://zhuanlan.zhihu.com/p/39341603
https://stackoverflow.com/questions/30596484/python-asyncio-context
https://www.python.org/dev/peps/pep-0555/

Guess you like

Origin www.cnblogs.com/allen2333/p/11465142.html