How do I kill a task / coroutine in Julia?

2019-04-19 04:13发布

using HttpServer

http = HttpHandler() do request::Request, response::Response
    show(request)
    Response("Hello there")
end

http.events["error"] = (client, error) -> println(error)
http.events["listen"] = (port) -> println("Listening on $port")
server = Server(http)

t = @async run(server, 3000)

This starts a simple little web server asynchronously. The problem is I have no idea how to stop it. I've been going through the Julia documentation and trying to find some function that will remove this task from the queue (kill, interrupt, etc.) but nothing seems to work.

How can I kill this task?

1条回答
孤傲高冷的网名
2楼-- · 2019-04-19 05:07

I don't see an official way to end a task specifically, but I think the general solution was the addition of throwto, which allows you to immediately schedule a task with a pending exception.

...
t = @async run(server, 3000)
...
ex = InterruptException()
Base.throwto(t, ex)
close(http.sock) # ideally HttpServer would catch exception to cleanup
查看更多
登录 后发表回答