Remix.run Logo
ashishb 7 hours 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?

nh2 an hour ago | parent | next [-]

Haskell 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.

ndriscoll 6 hours ago | parent | prev | next [-]

ZIO 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).

deathanatos 6 hours ago | parent | prev | next [-]

Python 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.)

lifis 6 hours ago | parent | prev | next [-]

All 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

perfmode 6 hours ago | parent | prev | next [-]

one of the reasons why i love writing control planes in Go.

nnx 6 hours ago | parent | prev | next [-]

in JS, signals and AbortController can replicate some of the functionality but it's far less ergonomic than Go.

https://github.com/ggoodman/context provides nice helpers that brings the DX a bit closer to Go.

drdaeman 6 hours ago | parent | prev | next [-]

C# has CancellationToken, but it’s just for canceling operations, not a general purpose context.

richbell 6 hours ago | parent | prev | next [-]

Kotlin Coroutine's structured concurrency. Cancelling a parent automatically cancels child jobs, unless explicitly handled not to. https://kotlinlang.org/docs/coroutines-basics.html

tadfisher 4 hours ago | parent [-]

Stupidly, child cancellation cancels the parent scope as well, unless the scope opts out by including SupervisorJob.

Quekid5 5 hours ago | parent | prev | next [-]

Java'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.

gzread 7 hours ago | parent | prev [-]

Not 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.