4 ms·
Context cancellation (and it's propagation) is one of the best features in Go. Is there any equivalent in major popular languages like Python, Java, or JS of t
by ashishb 7mo ago
Context cancellation (and it's propagation) is one of the best features in Go.
Is there any equivalent in major popular languages like Python, Java, or JS of this?
- gzread 7mo agoNot really, since they don't have `select` There's a stop_token in some Microsoft C++ library but it's not nearly as convenient to interrupt a blocking operation with it.
- ndriscoll 7mo agoZIO in Scala tracks this sort of thing except you don't have to remember to pass around or select on the ctx (it's just part of the fibre/"goroutine"); if it's cancelled, the fibre and its children just stops the next time it yields (so e.g. if it "selects" on anything or does any kind of IO).
- nnx 7mo agoin JS, signals and AbortController can replicate some of the functionality but it's far less ergonomic than Go. https://github.com/ggoodman/context https://github.com/ggoodman/context provides nice helpers that brings the DX a bit closer to Go.
- drdaeman 7mo agoC# has CancellationToken, but it’s just for canceling operations, not a general purpose context.
- perfmode 7mo agoone of the reasons why i love writing control planes in Go.
- deathanatos 7mo agoPython async tasks can be cancelled. But, I don't think you can attach must context to the cancel (I think you can pass a text message), so it would seem the argument of what go suffered from would apply. (I also think there's some wonkiness with and barriers to understanding Python's implementation that I don't think plagues Go to quite the same extent.)
- NeutralForest 7mo agoThe current meta is to use task groups and bubble up the exception that cancelled the coroutine/task.
- lifis 7mo agoAll mainstream languages have it in one or more forms (either direct task I/O cancellation, or cancellation tokens or I/O polling that can include synthetic events) since otherwise several I/O patterns are impossible
- richbell 7mo agoKotlin Coroutine's structured concurrency. Cancelling a parent automatically cancels child jobs, unless explicitly handled not to. https://kotlinlang.org/docs/coroutines-basics.html https://kotlinlang.org/docs/coroutines-basics.html
- tadfisher 7mo agoStupidly, child cancellation cancels the parent scope as well, unless the scope opts out by including SupervisorJob.
- Quekid5 7mo agoJava's Virtual Threads (JVM 21) + the Structured Concurrency primitives (not sure exactly what's available in Java 21+) do this natively. Also, a sibling poster mentioned ZIO/Scala which does the Structured Concurrency thing out of the box.
- nh2 7mo agoHaskell is the king of cancellation. Using asynchronous exceptions, you can cancel anything, anytime, with user -defined exception types so you know what the cancellation reason is. Example: maybeVal <— timeout 1000000 myFunction Some people think that async exceptions are a pain because you nerd to be prepared that your code can be interrupted any time, but I think it's absolutely worth it because in all the other languages I encounter progress bars that keep running when I click the cancel button, or CLI programs that don't react to CTRL+C. In Haskell, cancellability is the default and carries no syntax overhead. This is one of the reasons why I think Haskell is currently the best language for writing IO programs.
- neonsunset 7mo ago[dead]