总结
和threading.local()类似。Python3.7新增。
thread.local(): 不同线程,同一个变量保存不同的值。
contextvars: 不同上下文,同一个变量保存不同的值。例如:同一线程,不同的协程或者异步并发的任务(例如asyncio)的情况下同一个变量有不同的值。
参考
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/