• 博客园logo
  • 会员
  • 众包
  • 新闻
  • 博问
  • 闪存
  • 赞助商
  • HarmonyOS
  • Chat2DB
    • 搜索
      所有博客
    • 搜索
      当前博客
  • 写随笔 我的博客 短消息 简洁模式
    用户头像
    我的博客 我的园子 账号设置 会员中心 简洁模式 ... 退出登录
    注册 登录
返回主页

Rocinante

ASAP - always strive and prosper.
WTF - What's true for love, is true for code.
********************************Manjaro - yyds!********************************

  • 博客园
  • 首页
  • 新随笔
  • 联系
  • 订阅
  • 管理

Python - python3.7新增的contextvars vs Thread local(threading.local)

总结

和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/

posted @ 2019-09-05 12:20  Rocin  阅读(668)  评论(0)    收藏  举报
刷新页面返回顶部

公告

博客园  ©  2004-2025
浙公网安备 33010602011771号 浙ICP备2021040463号-3