根据Rust的要求杀死Warp Web服务器
我正在学习生锈,我想做的就是杀死或关闭 get /< /code>上的Web服务器。
这是您无法使用经纱做的事情吗?还是我的实施破坏了?
我有以下代码,但似乎不想响应任何HTTP请求。
pub async fn perform_oauth_flow(&self) {
let (tx, rx) = channel::unbounded();
let routes = warp::path::end().map(move || {
println!("handling");
tx.send("kill");
Ok(warp::reply::with_status("OK", http::StatusCode::CREATED))
});
println!("Spawning server");
let webserver_thread = thread::spawn(|| async {
spawn(warp::serve(routes).bind(([127, 0, 0, 1], 3000)))
.await
.unwrap();
});
println!("waiting for result");
let result = rx.recv().unwrap();
println!("Got result");
if result == "kill" {
webserver_thread.join().unwrap().await;
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
创建
async
块不会在内部执行代码;它只是创建未来
您需要.await
。您的服务器实际上从未运行。通常,使用与异步代码的线程无法正常工作。最好使用您的运行时任务,如果
warp
是tokio
,使用
tokio :: spawn(Spawn()
:
您也可能会发现有必要使用而不是同步通道。
Creating an
async
block is not going to execute the code inside; it is just creating aFuture
you need to.await
. Your server never actually runs.In general, using threads with async code is not going to work well. Better to use your runtime tasks, in case of
warp
it istokio
, usingtokio::spawn()
:You may also find it necessary to use tokio's async channels instead of synchronous channels.
您的代码中有两个问题:
(未经测试)完整示例:
There are two issues in your code:
bind_with_graceful_shutdown
so that you can notify the server to exit.(Untested) complete example: