fabric的两种并行方式
fabric是一种python类库,基于ssh协议的命令行工具用于应用部署和系统任务管理,下面来说一下fabric代码并行运行的两种方式:
1:参数 -P
from fabric.api import * host1 = 'root@ip1:22' host2 = 'root@ip2:22' env.hosts = [host1, host2 ] env.passwords = { host1: "p1", host2: "p2" } def backup_and_cp(): with cd("/home/**"): run('ls')
然后 运行 fab -P backup_and_cp就可以了
2:装饰器
import time from fabric.api import * host1 = 'root@ip1:22' host2 = 'root@ip2:22' env.hosts = [host1, host2 ] env.passwords = { host1: "p1", host2: "p2" } @parallel def backup_and_cp(): with cd("/home/**"): run('ls')
运行 fab backup_and_cp就OK了