该网站说:
关闭连接:Fabric 的连接缓存从不关闭连接本身——它留给正在使用它的任何东西。fab 工具会为您记账:它遍历所有打开的连接并在退出之前关闭它们(无论任务是否失败。)
图书馆用户需要确保他们在程序退出之前明确关闭所有打开的连接,尽管我们计划在未来使这更容易。
我到处搜索,但我找不到如何断开或关闭连接。我正在遍历我的主机并设置 env.host_string。它正在工作,但退出时挂起。关于如何关闭的任何帮助?重申一下,我使用的是库,而不是 fabfile。
织物的 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
所以我去关闭那些。
如果您不想遍历所有打开的连接,fabric.network.disconnect_all()
那就是您正在寻找的。文档字符串读取
"""
与所有当前连接的服务器断开连接。在主循环的末尾fab
使用,也供图书馆用户使用。
"""
您可以使用以下代码片段(使用结构 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()
from fabric.network import disconnect_all
disconnect_all()