{
  "id": 11363409,
  "title": "协程到底是什么？用\"一个人守三口锅\"讲明白，别再和线程混为一谈",
  "url": "https://urgent.news/2026/10/02/story-11363409",
  "topic": "tech",
  "section": "Tech",
  "published": "2026-10-02T05:22:50.000Z",
  "source": {
    "name": "Dev.to",
    "slug": "dev-to",
    "url": "https://dev.to/jianfeng_huang_5dc66197a0/xie-cheng-dao-di-shi-shi-yao-yong-ge-ren-shou-san-kou-guo-jiang-ming-bai-bie-zai-he-xian-cheng-hun-wei-tan-3j4h"
  },
  "original_language": "zh",
  "account": "What exactly is a coroutine and how does it differ from a thread? Many people mistakenly describe a coroutine as a lightweight user-level thread. However, this oversimplifies the concept. Unlike threads, which compete for CPU time and require careful handling of shared resources through locks, coroutines are designed for cooperative multitasking within a single thread. They can be paused and resumed without relying on the operating system or needing locks, making them efficient for I/O-bound tasks. The key differences lie in how they are scheduled and the trade-offs they entail. Coroutines give up control explicitly, allowing the runtime to manage their execution flow, while threads are preemptively scheduled by the OS. Understanding when to use coroutines versus threads depends on the nature of the workload—whether it's CPU-intensive or I/O-bound.",
  "summary": "面试里被问\"协程是什么\"，很多人的答案是一句背来的话：\"协程是用户态的轻量级线程。\" 这句话不算错，但它几乎解释不了任何事。听完之后你还是不知道该在什么时候用它、为什么它会被一个 time.sleep() 卡死、为什么它不需要锁。 这篇不讲定义，只讲三件事： 协程在切什么、调度权在谁手里、什么时候不该用。 一、先把三个词排成一队：进程、线程、协程 这三个东西经常被并列着讲，但它们回答的其实是同一个问题： \"谁来决定现在跑哪段代码？\" 进程 ：操作系统给一个程序划的地盘，有自己独立的内存空间。切进程最贵，因为要换页表、换地址空间。 线程 ：进程内部的执行流，共享同一片内存，但各自有独立的栈。切换要走操作系统（内核态），几十到几百纳秒起步，而且共享内存意味着你需要锁。 协程 ：跑在 某一个线程内部…",
  "key_points": [],
  "editors_take": "Coroutine differs from thread in cooperative multitasking within one thread without competing for CPU time or needing locks, making it efficient for I/O-bound tasks with explicit control give-up.",
  "illustration": null,
  "coverage": {
    "outlets": 1,
    "also_reported_by": []
  },
  "ai_generated": true,
  "disclaimer": "Summaries, key points and the editor’s take are written by software from other outlets’ reporting and may contain errors — always check the linked original."
}