主题: concurrency
-
Cancellation and deadlines in Go with context.Context
Thread a context.Context from the incoming request through every call that can block, derive child contexts with WithTimeout or WithCancel, always call the cancel function, and check ctx.Done() in loops so that a client disconnect or a deadline stops the whole tree of goroutines instead of leaving them running.
-
asyncio 何时有用,何时无益
asyncio 通过在 await 处挂起任务,在单个线程上运行大量受 I/O 限制的任务。它不会加速受 CPU 限制的代码,而协程中的阻塞调用会让整个事件循环停滞。
-
事务隔离级别的实际应用
读已提交、可重复读和可串行化在并发性与一致性之间作出权衡;了解各级别允许哪些异常,才能决定何时需要显式加锁或重试。
-
为 Python 工作负载选择线程、进程或 asyncio
先判断热点路径的性质:等待 I/O 适合 asyncio(大量连接、异步库)或线程池(少量阻塞调用);纯 Python 的 CPU 工作需要进程或自由线程构建;释放 GIL 的原生代码可使用线程。限制每个池的规模,显式选择进程启动方式,并编写关闭流程。
-
利用返回的修订号验证写后读取
通过稳定标识符和修订号确认写入结果,避免将并发产生的较新更新误认为自己提交内容的精确副本。
-
Handle stale write tokens as reread signals
Recover from a failed write precondition by rereading and rebuilding the intended change against the current version.
-
Preventing concurrent runs of an admin script: flock, lockf and a named mutex on Windows
A scheduled or agent-triggered admin script that can be started again before the previous run finished needs a lock, not a hope that it never happens. flock(1) on Linux and lockf(1) on FreeBSD tie a kernel-level advisory lock to a running process so it cannot go stale the way a leftover PID file can; Windows scripts get the equivalent with a named System.Threading.Mutex.
-
At what workload does the free-threaded CPython build beat a process pool for a mixed I/O and CPU service?
Open question: the free-threaded build removes the GIL but adds single-threaded overhead and may fall back to the GIL when an unprepared extension is imported, while process pools pay for pickling and memory duplication; for which CPU-to-wait ratios, working sets and core counts does a thread pool on the free-threaded build deliver more throughput per core?
-
C# async/await pitfalls: sync-over-async, async void and ConfigureAwait
The classic mistakes in C# asynchronous code are blocking on a Task with .Result, .Wait() or GetAwaiter().GetResult() (deadlocks under a single-threaded SynchronizationContext, thread-pool starvation on servers), async void methods whose exceptions cannot be caught, and misplacing ConfigureAwait(false), which belongs in general-purpose libraries and not in application code.
-
Java virtual threads in outline: what changes and what does not
JEP 444 (JDK 21) adds virtual threads: cheap threads scheduled by the JDK onto a small pool of carrier platform threads, which unmount while blocked on most JDK I/O so that thread-per-request code scales without an asynchronous style. They are not faster, must never be pooled, and until JEP 491 (JDK 24) blocking inside synchronized pinned the carrier.
-
Use expiring leases for distributed jobs
Protect a reclaimed job from a delayed previous worker with ownership checks and a monotonically increasing fencing token.
-
The GIL: what it serialises and what it does not make safe
The global interpreter lock lets only one thread execute Python bytecode at a time and protects the interpreter's own structures, not the program's invariants: read-modify-write sequences such as counter += 1 or check-then-set on a dict still need a threading.Lock. Free-threaded builds keep the same rule.
-
Goroutines, channels and the sync package: Go concurrency in outline
A go statement runs a function call concurrently on a cheap, growable stack; channels move values between goroutines and block until both sides are ready; sync.WaitGroup, Mutex and Once cover the cases where sharing memory is simpler. A data race is a bug whose outcome the memory model only partly constrains, and the -race detector is the tool for finding it.
-
在 Jev 决策之后、产生副作用之前重新验证状态
防止在相关记录、权限或策略已改变后,仍盲目应用针对应用先前状态作出的决策。
-
Go 竞态检测器的覆盖范围:无报错运行无法说明未执行路径的情况
将 go test -race 的结果与实际执行的并发路径关联起来,包括启动和关闭。
-
Tokio 的 Send 错误:检查跨越 await 保留的状态
检查哪些状态在挂起后仍然存在,以修复经 spawn 启动的 future,而不是添加 unsafe trait 实现。
-
Bound concurrency per host and account
Control simultaneous tool calls separately from request rate, with bounded queues and cancellation-safe permit release.
-
ThreadSanitizer 分诊:找回缺失的同步契约
阅读两条冲突的访问路径,修复将它们联系起来的所有权或同步规则。
机器可读: JSON