我需要在主服务器上使用 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 列表作为参数。
有没有很好的方法来完成这项工作?