I am planning to write a remote agent module as bellow:
from fabric.api import run as fab_run
from fabric.api import env as fab_env
class RemoteAgent(object):
def __init__(self, host, port, user, password):
self.host = host
self.port = port
self.user = user
self.password = password
def set_env(self):
fab_env.host_string = "%s:%s" % (self.host, self.port)
fab_env.user = self.user
fab_env.password = self.password
def run(self, cmd):
self.set_env()
return fab_run(cmd)
So I can use RemoteAgent
like this:
ra = RemoteAgent('192.168.1.102', 22, 'worker', 'password')
ra.run('killall java')
But RemoteAgent
can not be used concurrently, or there will be a race condition.
My question is: how can use fabric as a lib in a concurrent way?
It depends on type of concurrency you would like to use.
It's not a good idea to use fabric with thread concurrency. Fabric is not thread safe (see last question in FAQ).
There is no problem though using process concurrency with multiprocessing module. Fabric uses it internally to provide their Parallel execution functionality.