使用 API 时通过 Python Fabric 断开与主机的连接

发布于 2024-09-12 12:25:48 字数 368 浏览 4 评论 0原文

该网站说:

关闭连接:Fabric 的 连接缓存永远不会关闭 连接本身——它留下了这个 任何正在使用它的人。晶圆厂工具 为您记账吗:它 迭代所有打开的连接并且 在退出之前关闭它们 (无论任务是否 失败与否。)

图书馆用户需要确保他们 显式关闭所有打开的连接 在他们的程序退出之前,尽管我们 计划使这变得更容易 未来。

我已经到处搜索,但我找不到如何断开或关闭连接。我正在循环访问我的主机并设置 env.host_string。它正在工作,但退出时挂起。关于如何关闭有任何帮助吗?重申一下,我使用的是库,而不是 fabfile。

The website says:

Closing connections: Fabric’s
connection cache never closes
connections itself – it leaves this up
to whatever is using it. The fab tool
does this bookkeeping for you: it
iterates over all open connections and
closes them just before it exits
(regardless of whether the tasks
failed or not.)

Library users will need to ensure they
explicitly close all open connections
before their program exits, though we
plan to makes this easier in the
future.

I have searched everywhere, but I can't find out how to disconnect or close the connections. I am looping through my hosts and setting env.host_string. It is working, but hangs when exiting. Any help on how to close? Just to reiterate, I am using the library, not a fabfile.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

初相遇 2024-09-19 12:25:48

如果您不想遍历所有打开的连接,那么您需要的是fabric.network.disconnect_all()。文档字符串读取

“”“
断开与所有当前连接的服务器的连接。
fab主循环的末尾使用,也供库用户使用。

“”“

If you don't want to have to iterate through all open connections, fabric.network.disconnect_all() is what you're looking for. The docstring reads

"""
Disconnect from all currently connected servers.
Used at the end of fab's main loop, and also intended for use by library users.

"""

兰花执着 2024-09-19 12:25:48

Fabric 的 main.py 具有以下内容:

from fabric.state import commands, connections

for key in connections.keys():
    if state.output.status:
        print "Disconnecting from %s..." %, denormalize(key), connections[key].close()

fabric.state.connections 是一个字典,其值为:paramiko.SSHClient

所以我要关闭它们。

The main.py for fabric has this:

from fabric.state import commands, connections

for key in connections.keys():
    if state.output.status:
        print "Disconnecting from %s..." %, denormalize(key), connections[key].close()

fabric.state.connections is a dict with the value being: paramiko.SSHClient

So off I go to close those.

空‖城人不在 2024-09-19 12:25:48

您可以使用以下代码片段(使用 Fabric 1.10.1)按主机名断开与特定连接的连接:

def disconnect(host):
    host = host or fabric.api.env.host_string
    if host and host in fabric.state.connections:
        fabric.state.connections[host].get_transport().close()

You can disconnect from a specific connection, by host name, using the following code snippet (with fabric 1.10.1):

def disconnect(host):
    host = host or fabric.api.env.host_string
    if host and host in fabric.state.connections:
        fabric.state.connections[host].get_transport().close()
烟花肆意 2024-09-19 12:25:48
from fabric.network import disconnect_all
disconnect_all()
from fabric.network import disconnect_all
disconnect_all()
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文