如何在 Julia 中终止任务/协程? [英] How do I kill a task / coroutine in Julia?
本文介绍了如何在 Julia 中终止任务/协程?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
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)
这会异步启动一个简单的小型 Web 服务器.问题是我不知道如何阻止它.我一直在浏览 Julia 文档并试图找到一些可以从队列中删除此任务的函数(kill
、interrupt
等),但似乎没有任何效果.
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?
推荐答案
我没有看到具体结束任务的官方方法,但我认为一般的解决方案是 添加了 throwto, 允许您立即安排有未决异常的任务.
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
这篇关于如何在 Julia 中终止任务/协程?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文