如何在Julia中杀死任务/协程? [英] How do I kill a task / coroutine in Julia?

查看:123
本文介绍了如何在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文档,试图找到一些可以从队列中删除此任务的函数(killinterrupt等),但是似乎没有任何作用.

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.

如何杀死这个任务?

推荐答案

我没有看到专门结束任务的正式方法,但我认为一般的解决方案是

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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆