【问题标题】:How to execute different long-running tasks on different hosts at the same time in fabric?如何在fabric中同时在不同主机上执行不同的长时间运行的任务?
【发布时间】:2016-01-29 06:04:15
【问题描述】:

我对结构很陌生,我正在尝试创建一个脚本来启动三个不同的服务器和一个客户端,但它似乎在第一个任务时停止。这是我的代码:

env.user='userX'
env.roledefs = {
'client':['HostC'],
'id 0':['Host0'],
'id 1':['Host1'],
'id 2':['Host2']
}

def go():
execute(serve0)
execute(serve1)
execute(serve2)
execute(req)

@roles('id 0')
def serve0():
run('./go/bin/kvsd -id 0 -config-file ~/go/src/github.com/userX/kvs/kvsd/conf/config.ini')

@roles('id 1')
def serve1():
run('./go/bin/kvsd -id 1 -config-file ~/go/src/github.com/userX/kvs/kvsd/conf/config.ini')

@roles('id 2')
def serve2():
run('./go/bin/kvsd -id 2 -config-file ~/go/src/github.com/userX/kvs/kvsd/conf/config.ini')

@roles('client')
def req():
run('./go/bin/kvsc -config-file ~/go/src/github.com/userX/kvs/kvsc/config.ini')

我用“fab go”运行它,但这只会导致它执行serve0,它不会在没有中断的情况下停止,因此会阻止任何其他任务的执行。有没有办法让它们并行运行?另外,有没有更好的方法将特定任务与特定主机联系起来?

【问题讨论】:

    标签: python python-2.7 fabric


    【解决方案1】:

    @parallel 装饰器添加到您的函数中,它们将自动并行运行。更多细节在这里:http://docs.fabfile.org/en/1.10/usage/parallel.html

    从 cli 调用结构时使用-H 来确定要使用的主机。在任务中,您可以检查env 字典,或者您可以使用@hosts 装饰器来指定运行的位置。更多信息在这里:http://docs.fabfile.org/en/1.10/usage/execution.html

    【讨论】:

    • 我正在测试并行,通过使用一个小脚本:@parallel def go(): print "current host: %s" % env.host_string 但这给了我一个 EOFerror,它适用于单个当我删除并行装饰器时主机
    • 这个错误听起来像是报价不匹配之类的。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-11-27
    • 2011-01-19
    • 1970-01-01
    • 2017-12-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多