News for hackers. Stuff that matters.
Re:Async destructors, async genericity and completion futures by AgentME on Thursday June 02, 2022 @03:18AM (#31592327)
A good way across languages to cancel an asynchronous task is by passing a CancellationToken / AbortSignal (Javascript) as an argument. This scales nicely as a task can pass the token onward to any number of other tasks, and the original creator of the token can abort it and all uses of it at any time.
Re:Async destructors, async genericity and completion futures by zaphar on Friday June 03, 2022 @09:33AM (#31608020)
This is the primary use-case for Contexts in go. It's standard practice to pass contexts through all your concurrent code and abort on the `Cancel` call.
Re:Async destructors, async genericity and completion futures by sbf501 on Thursday June 02, 2022 @02:15PM (#31598556)
I remember a github repo like this from ages ago. How is the token used? I assume the token has a function that triggers an abort, but does that mean the downstream code has to constantly check the token, or does it simply generate a fault/exception at the lowest level that bubbles upstream? I'm not sure of the token mechanics.
Re:Async destructors, async genericity and completion futures by AgentME on Friday June 03, 2022 @01:03AM (#31604489)
It works like your first guess, but it's not so bad. Downstream code has to either check the token occasionally, subscribe to an event from it, or just pass it forward to another function which uses the token. So if you were writing a function that just made a series of network requests, you would make it take a CancellationToken as a parameter, and then you would pass that parameter on to each of the network request function calls inside your function. Then if the token is in the aborted state while any of the network function calls are in-progress, then that network function call will throw an exception which will bubble up through your function, without your function needing to do anything special with the CancellationToken directly.
Re:Async destructors, async genericity and completion futures by e12e on Thursday June 02, 2022 @09:29AM (#31594609)
Feels like this should be a language feature, though - like how python passes self for objects - or ruby binds self for you?
Re:Async destructors, async genericity and completion futures by AgentME on Monday June 06, 2022 @05:24PM (#31646495)
Kotlin has "structured concurrency" which I believe automatically handles some cases of this, but I don't fully understand it. I find explicit cancellation tokens without any magic easier to understand.