How do I kill a task / coroutine in Julia?

I have a totally same question as this:
How do I kill a task / coroutine in Julia? - Stack Overflow How do I kill a task / coroutine in Julia? - Stack Overflow

I tried the same way to kill the task of the answer like:

t = @async run(`server_run.sh`) # start a task
...
Base.throwto(t, InterruptException()) # stop the task

The task might be a external program written in another language.

But this didn’t work in Julia 1.1. (It was just stopping the processing at the line.)

Does anyone know a solution for it?

1 Like

What do you want to happen that you’re not seeing happen? If you’re missing the garbage collection of things the task allocated, you can just assign t = nothing and it’ll then eventually collect all that trash.

Hi. @mbauman I would like to kill the task which is executed by async.
But when I do nothing, the task is still running after finishing the julia script.

@AtsushiSakai run make a process.

julia> process = run(`sleep 10`, wait=false)
Process(`sleep 10`, ProcessRunning)

You can kill a process by kill.

process = run(`sleep 10`, wait=false)
process_running(process) # true
kill(process)
process_running(process) # false
4 Likes