【问题标题】:How do I add timeout while using fabric for below code?如何在为以下代码使用结构时添加超时?
【发布时间】:2019-09-15 13:08:19
【问题描述】:

代码如下。我会在这里解释。

我使用结构检查 2 个服务器的挂载点状态。命令是

df -h /mnt

这个命令的问题是它可能永远挂在那里,所以结构代码会永远挂起。 我认为一些超时机制可以解决这个问题。但我没有在面料文档上找到它。有没有关于这个的设置?

import fabric
from fabric import ThreadingGroup as Group

directory = '/mnt'
group = Group('server1', 'server2', user='someuser', connect_kwargs={'password':'somepassword'})
try:
    result = group.run("df -h %s" % directory)
except fabric.exceptions.GroupException as e:
    err = e
    pass

【问题讨论】:

    标签: python invoke fabric linux-disk-free


    【解决方案1】:

    选项 #1:使用 bash 超时停止命令 timeout 10 df -h

    选项#2:使用run 函数的timeout parameter。 timeout 以秒为单位指定,如果命令超时,则会引发CommandTimeout 异常。

    Group.run

    在所有成员连接上执行Connection.run

    and Connection.run

    包装invoke.runners.Runner.run 的支持SSH 的实现;有关详细信息,请参阅其文档。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-11-22
      • 2021-07-25
      • 2021-03-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-02-22
      相关资源
      最近更新 更多