如何防止织物成型等待过程中返工



我总是使用fabric将我的进程从本地pc部署到远程服务器。

如果我有一个这样的python脚本:

test.py:

import time
while True:
    print "Hello world."
    time.sleep(1)

显然,这个脚本是一个连续运行的脚本。我将这个脚本部署到远程服务器,并像这样执行我的fabric脚本:

...
sudo("python test.py")

fabric将始终等待test.py的返回,并且不会退出。如何立即停止fabric脚本并忽略test.py

的返回?

通常对于这种异步任务处理来说,芹菜是首选。这里详细解释了芹菜和织物的结合使用。

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

sudo("python test.py 2>/dev/null >/dev/null &")

或将输出重定向到其他文件而不是/dev/null

这段代码为我工作:

fabricObj.execute("(nohup python your_file.py > /dev/null < /dev/null &)&")

其中fabricObj是fabric类的对象(内部定义),它与fabric代码对话。

最新更新