TE
TechEcho
Home24h TopNewestBestAskShowJobs
GitHubTwitter
Home

TechEcho

A tech news platform built with Next.js, providing global tech news and discussions.

GitHubTwitter

Home

HomeNewestBestAskShowJobs

Resources

HackerNews APIOriginal HackerNewsNext.js

© 2025 TechEcho. All rights reserved.

The contextvars and the chain of asyncio tasks in Python

2 pointsby socrateslee9 months ago

1 comment

socrateslee9 months ago
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.