在python中以一定顺序排列任务

时间:2019-06-18 10:06:16

标签: python algorithm

我有一个字典,描述了一堆作业,这些作业将按一定顺序处理:

jobs = {
"job_a": {"commands": ["ls -a", "echo $LANG"], "dependencies": []}
"job_b": {"commands": ["ps aux", "find -name xxx"], "dependencies": []}
"job_c": {"commands": ["umask", "cat yyy"], "dependencies": ["job_a"]}
"job_d": {"commands": ["kill -9 www", "sed zzz"], "dependencies": ["job_b", "job_c"]}
}

每个作业项目中的依存关系列表决定了它们应该执行的顺序:例如job_d应该在job_bjob_c之后执行。

我需要的是一个指示这些工作顺序的列表,在上面的示例中,该列表可能是

jobs_order = ["job_a", "job_b", "job_c", "job_d"]
# or 
jobs_order = ["job_a", "job_c", "job_b", "job_d"]
# or 
jobs_order = ["job_b", "job_a", "job_c", "job_d"]

最简单的方法是一遍又一遍遍遍jobs.items()列表,并将每次依赖项插入到前面的位置。

这里的工作量可能非常大。我们可以用更小的空间和更短的时间来做得更好吗?

1 个答案:

答案 0 :(得分:1)

它可能看起来像这样:

#!/usr/bin/env python3

from collections import defaultdict
from queue import SimpleQueue


def get_order(jobs):
    q = SimpleQueue()
    subtasks = defaultdict(list)
    n_deps = {}
    for key, job in jobs.items():
        dependencies = job.get('dependencies', [])

        n_deps[key] = len(dependencies)
        if len(dependencies) == 0:
            q.put(key)

        for parent in dependencies:
            subtasks[parent].append(key)

    order = []
    while not q.empty():
        job = q.get()
        order.append(job)
        for key in subtasks[job]:
            n_deps[key] -= 1
            if n_deps[key] == 0:
                q.put(key)

    return order


def main():
    jobs = {
        "job_d": {"commands": ["kill -9 www", "sed zzz"], "dependencies": ["job_b", "job_c"]},
        "job_c": {"commands": ["umask", "cat yyy"], "dependencies": ["job_a"]},
        "job_b": {"commands": ["ps aux", "find -name xxx"], "dependencies": []},
        "job_a": {"commands": ["ls -a", "echo $LANG"], "dependencies": []},
    }
    print(get_order(jobs))


if __name__ == '__main__':
    main()

输出:

['job_b', 'job_a', 'job_c', 'job_d']