TE
科技回声
首页24小时热榜最新最佳问答展示工作
GitHubTwitter
首页

科技回声

基于 Next.js 构建的科技新闻平台,提供全球科技新闻和讨论内容。

GitHubTwitter

首页

首页最新最佳问答展示工作

资源链接

HackerNews API原版 HackerNewsNext.js

© 2025 科技回声. 版权所有。

The contextvars and the chain of asyncio tasks in Python

2 点作者 socrateslee10 个月前

1 comment

socrateslee10 个月前
When we say “the contextvars is somehow like thread local, but mostly for coroutines”, we think that coroutines are just another types of threads. But actually we are not right. The coroutines are not threads. A thread has its own life cycle and its own stack space, so a thread local storage actually belongs to a thread. But a coroutine is just a function that can be paused and resumed, scheduled by a loop. So the context of a coroutine belongs not to the coroutine itself. When in a chain of coroutines, like the calling chain of Tasks, we need to be aware that the consistency of context are manually kept by the users, not automatically kept by the system.