通常对于这种异步任务处理,Celery是首选。
这详细解释了Celery和Fabric一起使用。
from fabric.api import hosts, env, execute,run
from celery import task
env.skip_bad_hosts = True
env.warn_only = True
@task()
def my_celery_task(testhost):
host_string = "%s@%s" % (testhost.SSH_user_name, testhost.IP)
@hosts(host_string)
def my_fab_task():
env.password = testhost.SSH_password
run("ls")
try:
result = execute(my_fab_task)
if isinstance(result.get(host_string, None), BaseException):
raise result.get(host_string)
except Exception as e:
print "my_celery_task -- %s" % e.message