Task 如何在Julia中终止任务/协同程序?

Task 如何在Julia中终止任务/协同程序?,task,julia,coroutine,Task,Julia,Coroutine,这将异步启动一个简单的小型web服务器。问题是我不知道如何阻止它。我一直在浏览Julia文档,并试图找到一些函数将此任务从队列中删除(kill,interrupt,等等),但似乎什么都不起作用 如何结束这项任务?我看不到一种正式的方法来专门结束一项任务,但我认为一般的解决方案是,允许您立即安排一项任务,但有一个挂起的异常 using HttpServer http = HttpHandler() do request::Request, response::Response show(

这将异步启动一个简单的小型web服务器。问题是我不知道如何阻止它。我一直在浏览Julia文档,并试图找到一些函数将此任务从队列中删除(
kill
interrupt
,等等),但似乎什么都不起作用


如何结束这项任务?

我看不到一种正式的方法来专门结束一项任务,但我认为一般的解决方案是,允许您立即安排一项任务,但有一个挂起的异常

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)

解决问题的一个简单方法是使用可取消循环重新实现。Julia使用libuv,因此
accept
确实是这样。请看,特别是…谢谢,但我实际上想找一个更一般的。HTTP服务器只是异步运行任务的一个示例。
...
t = @async run(server, 3000)
...
ex = InterruptException()
Base.throwto(t, ex)
close(http.sock) # ideally HttpServer would catch exception to cleanup