Urgent.News

What's breaking now, across thousands of outlets.

Tech

What is a coroutine, anyway? Explain it clearly with an analogy of one person guarding three pots, and stop confusing it with threads.

In an interview, when asked what a coroutine is, many people's answer is a memorized sentence: "A coroutine is a user-state lightweight thread." This sentence is not wrong, but it hardly explains anything. After hearing it, you still don't know when to use it, why it gets stuck with a time.sleep(), and why it doesn't need a lock. This article doesn't discuss definitions, but rather three things: what coroutines switch, who holds the scheduling right, and when not to use them. First, let's put three terms in a row: process, thread, and coroutine. These three things are often discussed together, but they actually answer the same question: Who decides which piece of code to run now? Process: The operating system allocates a space for a program, which has its own independent memory space. Switching processes is expensive because it requires changing the page table and address space. Thread: An execution flow within a process that shares the same memory but has its own independent stack. Switching threads requires the operating system (kernel mode), starting from tens to hundreds of nanoseconds, and shared memory means you need locks. Coroutine: Runs inside a thread...

Translated from Chinese Read in Chinese

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.

Written by urgent.news from Dev.to's reporting — not their text. Machine-written — may contain errors; check the original before relying on it.

Read the original at dev.to →

More in Tech

Kenya records 52.3M smartphones as mobile use expands

Kenya had 52.3 million smartphones connected to mobile networks by the end of June 2026, highlighting the growing role of mobile technology in the daily lives of Kenyans.

  • Kenya recorded 52.3 million smartphones connected to mobile networks by June 2026.
  • Smartphones are used for financial transactions, business, education, and services.
  • Mobile data usage increased by 6% to 800 million gigabytes in Q3 2025/2026.

An alternate-key lookup still needs object authorization

Most BOLA writeups show GET /orders/{id} with a guessed UUID. The same bug often hides behind a “friendlier” lookup: GET /orders?email= , findByExternalId , or where slug = ? .

  • Alternate-key lookups can expose object authorization flaws.
  • System should authorize loaded resource for specific subject.
  • Same denial shape as id-based routes prevents accidental oracles.

What are coroutines, exactly? Just think of them as "functions that can pause in the middle," and everything becomes clear.

TL;DR:协程不是“更快的线程”,它是一个可以在中途停下来、把线程让给别人、之后还能从停下的那一行接着跑的函数。它省掉的不是计算时间,而是等 IO 时的空转。 面试被问“协程是什么”,大多数人的答案是背来的一句:“协程是用户态的轻量级线程。” 这句话不算错,但它几乎解释不了任何事。听完你还是不知道该在什么时候用它、为什么它会被一个 time.sleep() 卡死、为什么它不需要锁。…

More from Friday 2 October →