fabric env.hosts未被识别

时间:2017-03-22 14:28:37

标签: python parallel-processing fabric

我有一个简单的fabfile名称 的 env_fabfile.py

# env_fabfile.py
# makes use of fab env variables 

from fabric.api import env, run
def login():
    env.hosts = ['user@host1:1234', 'user@host2:2345']
    env.passwords = {'user@host1:1234': 'pass1', 'user@host2:2345': 'pass2'}
    env.parallel=True

def run_lsb_release():
    run('lsb_release -a')

现在我使用fab命令运行以上命令:

  

fab -f env_fabfile.py登录run_lsb_release

它完美地运行(并行)并提供所需的输出

现在我想实际计算相同脚本在串行运行时与在并行运行时之间的时差。所以为了做到这一点,我写了下面的python脚本: 的 timecal.py

# timecal.py
# runs the fabfile once in serial and calculates the time
# then runs the same file in parallel and calculates the time

from fabric.api import env, run
import time

def login():
    print "in login"
    env.hosts = ['user@host1:1234', 'user@host2:2345']
    env.passwords = {'user@host1:1234': 'pass1', 'user@host2:2345': 'pass2'}

def parallel(status):
    print "in parallel"
    env.parallel=status

def run_lsb_release():
    print "in run"
    run('lsb_release -a')

def do_serial():
    start_time = time.time()
    parallel(False)
    login()
    run_lsb_release()
    elapsed_time = time.time() - start_time
    return elapsed_time

def do_parallel():
    start_time = time.time()
    parallel(True)
    login()
    run_lsb_release()
    elapsed_time = time.time() - start_time
    return elapsed_time


if __name__ == '__main__':  
    print "Running in serial mode "
    print "Total time taken ", do_serial()

    print "Running in parallel mode"
    print "Total time taken ", do_parallel()

但是当我将timecal.py作为

运行时
  

python timecal.py

我在stdout上得到了以下内容(除了代码中的print语句)

  

找不到主机。请指定(单个)主机字符串以进行连接:

我不明白为什么?如何纠正脚本以便我能够实现我想要的(如上面的问题中所述)

如果我尝试使用不同版本的timecal.py,请:

from fabric.api import run, settings, env
import time

def do_parallel():
    start_time = time.time()
    env.hosts = ['user@host1:1234', 'user@host2:2345']
    env.passwords = {'user@host1:1234': 'pass1', 'user@host2:2345': 'pass2'}
    env.parallel=True
    run('lsb_release -a')
    elapsed_time = time.time() - start_time
    return elapsed_time

def do_serial():
    start_time = time.time()
    with settings(host_string='host1', port=1234, user='user', password='pass1'):
        run('lsb_release -a')
    with settings(host_string='host2', port=2345, user='user', password='pass2'):
        run('lsb_release -a')
    elapsed_time = time.time() - start_time
    return elapsed_time

if __name__ == '__main__':  
    try:
        print "Running in parallel mode"
        print "Total time taken ", do_parallel()

        print "Running in serial mode "
        print "Total time taken ", do_serial()
    except Exception as e:
        print e

我收到以下错误:

  

致命错误:需要提示目标主机连接字符串(主机:无),但输入在并行模式下不明确

我不明白为什么主持人:没有?代码有什么问题?

1 个答案:

答案 0 :(得分:1)

简短的回答是,您不应该按照当前的方式设置<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script> <input type="checkbox" class="product-check" id="package1"> <input type="checkbox" class="product-check" id="package2"> <div id="pg-review"></div> <div id="tp-review"></div> <div id="pack-1"> <p>Package 1 Section</p> </div> <div id="tp-package-wrap"> <div class="tp-package"> <div class="product-wrap"> <label for="tp-bronze" class="package-check-toggle tp-package-push"> <h2 class="tp-package-title">A</h2> <p class="package-cost">$10</p> </label> <input type="checkbox" class="tp-pack-check" data-solo-price="10" data-combined-price="8"> </div> </div> <div class="tp-package"> <div class="product-wrap"> <label for="tp-silver" class="package-check-toggle tp-package-push"> <h2 class="tp-package-title">B</h2> <p class="package-cost">$15</p> </label> <input type="checkbox" class="tp-pack-check" data-solo-price="15" data-combined-price="13"> </div> </div> <div class="tp-package"> <div class="product-wrap"> <label for="tp-gold" class="package-check-toggle tp-package-push"> <h2 class="tp-package-title">C</h2> <p class="package-cost">$20</p> </label> <input type="checkbox" class="tp-pack-check" data-solo-price="20" data-combined-price="18" data-package="Gold"> </div> </div> </div>值,并且env.hosts是超级粗略的(可能会损坏?)和{{3} },尤其是it's recommended to use SSH key-based access

以下是timecal.py脚本的修改版本,该脚本按预期工作,我将在下面列出一些差异。

env.passowrds

主要区别在于使用# timecal.py # runs the fabfile once in serial and calculates the time # then runs the same file in parallel and calculates the time from fabric.api import env, run, execute, parallel import time env.use_ssh_config = True env.roledefs = { "my_servers": ['server_1', 'server_2'] } def run_lsb_release(): print "in run" run('lsb_release -a') def do_task(task_func): start_time = time.time() execute(task_func, roles=['my_servers']) elapsed_time = time.time() - start_time return elapsed_time if __name__ == '__main__': print "Running in serial mode " print "Total time taken ", do_task(run_lsb_release) print "Running in parallel mode" print "Total time taken ", do_task(parallel(run_lsb_release)) 和SSH配置文件,而不是主机&amp;密码。由于这些任务在不同的线程中执行,因此这些值在并行执行模式下不起作用。 leveraging native SSH config files有点薄,但这基本上就是你遇到这个问题的原因。