-
-
Notifications
You must be signed in to change notification settings - Fork 5.5k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
API Request : Interrupt and terminate a task #6283
Comments
Related: #4037 |
Also related: #1700 We can already do this using |
We should probably still have |
Nah, that won't be sufficient anyway. We should remove the task from whatever wait queue it is in, so it can be GC'd. |
Bumping. Do we have this functionality yet? Do we want it? |
Don't have it yet. We should. Killing a task should also release whatever resource it is waiting on - file fd, socket, remote reference, etc. |
I don't think we should add this, since "releasing whatever resource" is generally impractical and buggy. I don't know of any APIs that have this sort of feature but don't warn you not to use it due to the infeasibility of cleaning up state afterwards: |
After reading those links, an appropriate solution would be to define an Currently it should just throw an |
+1 for |
|
Another reference on this topic is https://news.ycombinator.com/item?id=13470452 Note, that the current preferred mechanism of aborting another Task is to |
That works for libuv resources implementing |
No, it would still be different because it would no longer be specific to intended interruption. For example, you might end up aborting a call to I think the |
The calls return a In a statement like |
Right, but I thought that's why |
Someone has written an article that draws an equivalence of https://vorpus.org/blog/notes-on-structured-concurrency-or-go-statement-considered-harmful There is a Julia-specific thread to this conversation at https://discourse.julialang.org/t/schedule-considered-harmful/10540 As someone who learned programming on old-fashioned BASICs like Applesoft BASIC on the Apple II+, and then had to move to procedural programming in C, I initially had the same gut-response to this article as I did back then. I'd call it "instinctive repulsion". But of course we do all now recognize In doing so, I have come to the conclusion that the article makes some great points. There is no good way to universally handle uncaught exceptions at the top of the But the reason I mention this article in this particular discussion is that I think having to support such unbounded Anyhow, just some food for thought here. I'm not necessarily proposing anything, but rather hoping to move the idea of task cancellation back into active discussion. |
I think we should look closely at the Trio approach to I/O in the future—i.e. post 1.0 (so this may have to be optional in 1.x or it may have to wait until 2.0). It has some really nice properties, including:
The most effective way forward may be to see if I can get @JeffBezanson and @njsmith in a room together some time since Nathaniel is a much more effective and explainer of and advocate for the Trio model than I am. (Hope you don't mind the ping, Nathaniel!) |
Sounds like a fun time to me :-) |
BTW, we've just created a virtual room for cross-language discussions of structured concurrency, in case anyone is interested: https://trio.discourse.group/c/structured-concurrency |
Cool, Nathan, I’ve joined :) |
Nice, I've joined as well. Reading over the blog post I think this approach makes a lot of sense. (As a side note — it also means that it was "correct" to inherit loggers from their parent task. Phew!) |
I think cancellation in Trio works nicely because Python forces you to write Is there any plans/ideas for (1) how to make non-I/O (compute-intensive) functions cancellable and (2) how to mark them as such in a way that the callers can know that they have to prepare for cancellation? I guess passing around cancellation tokens (see also https://vorpus.org/blog/timeouts-and-cancellation-for-humans/) and handling exit manually as in Go's BTW, I played around a bit to see how Trio-like API would look like and how passing around "nursery" would work (although it's more like Go's function bg(nursery)
@with_nursery nursery begin
println("in nursery")
Threads.@spawn begin
sleep(0.1)
Threads.@spawn begin
sleep(0.1)
println("world")
end
println("hello")
end
end
println("out of nursery")
end
function demo()
@sync begin
println("launching background tasks")
bg(@get_nursery)
bg(@get_nursery)
println("synchronising background tasks")
end
println("synchronized background tasks")
end Quick-and-dirty implementation of `@get_nursery` and `@with_nursery` (no cancellation support at all!))struct TaskVector
tasks::Vector{Any}
lock::Threads.SpinLock
end
TaskVector(tasks) = TaskVector(tasks, Threads.SpinLock())
function Base.push!(v::TaskVector, x)
lock(v.lock)
try
return push!(v.tasks, x)
finally
unlock(v.lock)
end
end
macro get_nursery()
var = esc(Base.sync_varname)
quote
TaskVector($var)
end
end
macro with_nursery(nursery, body)
ex = quote
let $(Base.sync_varname) = $nursery
$body
end
end
return esc(ex)
end
|
I think Kotlin could be interesting here since it does structured concurrency without But it looks there is no mechanism for callers to know if the function is cancellable? |
I started implementing a very minimal version of structured concurrency here https://github.com/tkf/Awaits.jl. Basically I implemented what I mentioned in the last bits of #32677 (comment). The idea is to define a macro ans = $body
if ans isa Exception
cancel!($context)
return ans
end
ans where shouldstop($context) && return Cancelled() where context = @cancelscope begin
@go ...
@go ...
end
cancel!(context) I also wrote some minimal documentation https://tkf.github.io/Awaits.jl/dev/ and tests https://github.com/tkf/Awaits.jl/blob/master/test/test_simple.jl |
Is this the reason I cannot stop a FileEvent (BetterFileWatching.jl) task and it keeps on working in the background w/e I do? Kind regards |
No, that just sounds like a BetterFileWatching.jl API problem. The stdlib FileWatching shouldn't have that issue |
Reference : https://groups.google.com/d/msg/julia-users/12eYgWWLzfY/7fxXH5cWX2MJ
The text was updated successfully, but these errors were encountered: