Salt - 如何异步获取cmd_async输出

时间:2013-10-17 14:26:06

标签: python asynchronous salt-stack

我需要在master上使用salt-api同时运行多个salt命令。 当我想以异步方式获取输出时,问题出现了。

假设我在跑步者中有以下代码(更像是伪)(仅为了示例):

client = salt.client.LocalClient()
for fun in funs:
    jid = client.cmd_async(target, fun, [arg])
    jobs.append(jid)

out = {}
while len(jobs):
    for jid in jobs:
        # this can be any function that can in some way assure me about the state
        # whether the command it's still running or it finished the job
        state = get_salt_cmd_state(jid)

        # checking if that command is really finished
        # in order to get it's output
        if state == FINISHED:
            out[jid] = get_salt_cmd_output(jid)
            jobs.remove(jid)

在github(salt/client/init.py)上查看salt,你可以找到一些方法来做到这一点,但是大多数方法至少部分阻塞,并且还需要将minions列表作为参数。

有没有一种很好的方法可以让它发挥作用?

1 个答案:

答案 0 :(得分:3)

我认为您要找的是get_cache_returns()中的salt/client/__init__.py方法。看起来它根本不会阻塞,但返回给定jid的作业缓存的内容。如果它返回一个空的dict,那么如果我正确地遵循代码,那么该作业就不完整了。

相关问题