【问题标题】:Salt - How to get cmd_async output asynchronouslySalt - 如何异步获取 cmd_async 输出
【发布时间】:2015-12-09 16:09:51
【问题描述】:

我需要在 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 列表作为参数。

有没有很好的方法来完成这项工作?

【问题讨论】:

    标签: python asynchronous salt-stack


    【解决方案1】:

    我认为您正在寻找的是 salt/client/__init__.py 中的 get_cache_returns() 方法。看起来它根本没有阻塞,而是返回给定 jid 的作业缓存的内容。如果它返回一个空字典,那么如果我正确地遵循了代码,那么这项工作就没有完成。

    【讨论】:

    • 感谢您的提示。我会仔细看看的。该方法中我关心的是以下几行:while fn_ not in ret: try: ret_data=self.serial.load(salt.utils.fopen(retp, 'r')) ...
    猜你喜欢
    • 2016-01-18
    • 2013-05-04
    • 1970-01-01
    • 2012-02-17
    • 1970-01-01
    • 2021-07-15
    • 1970-01-01
    • 2021-12-19
    • 1970-01-01
    相关资源
    最近更新 更多