首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何关闭tokio-postgres连接?

如何关闭tokio-postgres连接?
EN

Stack Overflow用户
提问于 2021-04-19 19:14:53
回答 1查看 203关注 0票数 1

在第一个示例的tokio-postgres documentation中,有一个示例显示您应该在单独的线程中运行数据库连接:

代码语言:javascript
复制
// The connection object performs the actual communication with the database,
// so spawn it off to run on its own.
tokio::spawn(async move {
    if let Err(e) = connection.await {
        eprintln!("connection error: {}", e);
    }
});

如果你这样做了,你怎么能在之后终止这种连接呢?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2021-04-19 21:48:08

如果您使用的是tokio 1,tokio::task::JoinHandle有一个abort()函数可以取消任务,从而断开连接。

代码语言:javascript
复制
let handle = task::spawn(async move {
    if let Err(e) = connection.await {
        eprintln!("connection error: {}", e);
    }
}
handle.abort(); // this kills the task and drops the connection

按原样使用我的代码片段将立即终止任务,因此这可能不是您最终想要的,但如果您保留handle并使用它,例如结合某种关闭侦听器,您应该能够控制所需的连接。

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/67160923

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档