How do I kill a task / coroutine in Julia?

狂风中的少年 提交于 2019-12-06 17:46:28

问题


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:


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


来源:https://stackoverflow.com/questions/27209663/how-do-i-kill-a-task-coroutine-in-julia

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!