python – Networkx作为任务队列?
发布时间:2020-12-20 13:10:58 所属栏目:Python 来源:网络整理
导读:我在networkx中有一个有向无环图.每个节点代表一个任务,节点的前驱是任务依赖(给定的任务在其’依赖性已执行之前)不能执行. 我想在异步任务队列中“执行”图形,类似于celery提供的(因此我可以轮询作业的状态,检索结果等). Celery不提供创建DAG的能力(据我所
我在networkx中有一个有向无环图.每个节点代表一个任务,节点的前驱是任务依赖(给定的任务在其’依赖性已执行之前)不能执行.
我想在异步任务队列中“执行”图形,类似于celery提供的(因此我可以轮询作业的状态,检索结果等). Celery不提供创建DAG的能力(据我所知)并且能够在所有依赖关系完成后立即执行任务(DAG可能有多个路径,即使一个任务很慢/阻止,有可能继续其他任务等). 有没有简单的例子说明我如何实现这一目标,或者甚至将networkx与芹菜整合? 解决方法
我认为这个功能可能有所帮助:
# The graph G is represened by a dictionnary following this pattern: # G = { vertex: [ (successor1: weight1),(successor2: weight2),... ] } def progress ( G,start ): Q = [ start ] # contain tasks to execute done = [ ] # contain executed tasks while len (Q) > 0: # still there tasks to execute ? task = Q.pop(0) # pick up the oldest one ready = True for T in G: # make sure all predecessors are executed for S,w in G[T]: if S == task and and S not in done:# found not executed predecessor ready = False break if not ready : break if not ready: Q.appen(task) # the task is not ready for execution else: execute(task) done.appen(task) # execute the task for S,w in G[task]:# and explore all its successors Q.append(S) (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |