【问题标题】:How do I kill a task / coroutine in Julia?如何在 Julia 中终止任务/协程?
【发布时间】:2015-01-28 08:28:09
【问题描述】:
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 文档并试图找到一些可以从队列中删除此任务的函数(killinterrupt 等),但似乎没有任何效果。

我怎样才能终止这个任务?

【问题讨论】:

  • 解决问题的一个简单方法是使用可取消的循环重新实现run。 Julia 使用 libuv,所以 accept 真的是 uv_accept。见特别。 uv_connection_cb.
  • 谢谢,但我实际上正在寻找比这更一般的东西。 HTTP 服务器只是异步运行任务的一个示例。

标签: task julia coroutine


【解决方案1】:

我没有看到专门结束任务的官方方法,但我认为一般的解决方案是the addition of throwto,,它允许您立即安排有待处理异常的任务。

...
t = @async run(server, 3000)
...
ex = InterruptException()
Base.throwto(t, ex)
close(http.sock) # ideally HttpServer would catch exception to cleanup

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-11-12
    • 2014-05-04
    • 1970-01-01
    • 2017-09-23
    相关资源
    最近更新 更多