jra*_*amm 8 python task-queue networkx celery directed-acyclic-graphs
我有一个有向无环图networkx。每个节点代表一个任务,节点的前驱是任务依赖项(给定任务在其依赖项执行之前无法执行)。
我想在异步任务队列中“执行”图表,类似于提供的celery服务(以便我可以轮询作业的状态、检索结果等)。Celery 不提供创建 DAG 的能力(据我所知),并且在所有依赖项完成后立即转移到 a 的能力task将是至关重要的(DAG 可能有多个路径,即使一个任务很慢/阻塞) ,可能会继续执行其他任务等)。
有没有任何简单的例子说明我如何实现这一目标,或者甚至networkx与集成celery?
nin*_*701 -1
我认为这个功能可能会有所帮助:
# 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)
Run Code Online (Sandbox Code Playgroud)