使用线程在不同主机上并行执行 Fabric 运行 命令
Fabric running commands on different hosts in parallel using threads
我在远程主机上使用 fab to 运行 命令。我无法使用内置的 fab 并行执行模式,因为我的代码是由我无法控制的某些库调用的。它创建多个线程并使用不同的主机参数调用我的 driver(host) 方法。
下面是我如何尝试实现它的代码示例。
def setup_env_for_fab(host, user):
env.host_string = host
env.user = user
def run_command():
run("python some_program.py")
def driver(host):
setup_env_for_fab(host, "ubuntu")
run_command()
所以driver(host) 函数可以被多个具有不同主机参数的线程调用。如果 driver(host) 被不同的线程同时调用,这个 运行 会在不同的主机上并行执行命令吗?
如果这不能按预期工作,我该如何实现?
如果你真的不需要,请不要手动滚动。 Fabric 使用 multiprocessing
, e.g. using the -P
CLI 标志内置并行执行:
$ fab -H web1,web2,web3 -P driver
有关所有选项,请参阅 Fabric docs on parallel execution。
关于线程支持,请参阅FAQ - Is Fabric thread-safe?。当前的 Fabric 1.10 仍然不是线程安全的:
Is Fabric thread-safe?
Currently, no, it’s not – the present version of Fabric relies heavily on shared state in order to keep the codebase simple. However, there are definite plans to update its internals so that Fabric may be either threaded or otherwise parallelized so your tasks can run on multiple servers concurrently.
更新:
结构现在是线程安全的:Upgrading from 1.x
我在远程主机上使用 fab to 运行 命令。我无法使用内置的 fab 并行执行模式,因为我的代码是由我无法控制的某些库调用的。它创建多个线程并使用不同的主机参数调用我的 driver(host) 方法。
下面是我如何尝试实现它的代码示例。
def setup_env_for_fab(host, user):
env.host_string = host
env.user = user
def run_command():
run("python some_program.py")
def driver(host):
setup_env_for_fab(host, "ubuntu")
run_command()
所以driver(host) 函数可以被多个具有不同主机参数的线程调用。如果 driver(host) 被不同的线程同时调用,这个 运行 会在不同的主机上并行执行命令吗? 如果这不能按预期工作,我该如何实现?
如果你真的不需要,请不要手动滚动。 Fabric 使用 multiprocessing
, e.g. using the -P
CLI 标志内置并行执行:
$ fab -H web1,web2,web3 -P driver
有关所有选项,请参阅 Fabric docs on parallel execution。
关于线程支持,请参阅FAQ - Is Fabric thread-safe?。当前的 Fabric 1.10 仍然不是线程安全的:
Is Fabric thread-safe?
Currently, no, it’s not – the present version of Fabric relies heavily on shared state in order to keep the codebase simple. However, there are definite plans to update its internals so that Fabric may be either threaded or otherwise parallelized so your tasks can run on multiple servers concurrently.
更新:
结构现在是线程安全的:Upgrading from 1.x