2016-03-20 36 views
4

我在networkx有一個定向的非循環圖。每個節點代表一個任務,一個節點的前任是任務依賴(一個給定的任務直到它的依賴執行完畢才能執行)。Networkx作爲一個任務隊列?

我想在異步任務隊列中「執行」圖形,類似於celery提供的圖形(以便我可以輪詢作業狀態,檢索結果等)。 Celery沒有提供創建DAG的能力(據我所知),並且只要所有依賴關係完成就能夠移動到task將是至關重要的(DAG可能有多條路徑,並且即使一個任務緩慢/阻塞,有可能轉移到其他任務等)。

是否有任何簡單的例子,我可以如何實現這一目標,或者甚至可以集成networkxcelery

+2

你可能會尋找被稱爲DASK:http://dask.pydata.org/en/latest/custom-graphs.html?highlight=graph – denfromufa

回答

0

我覺得這個功能可以幫助:

# 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) 
+0

你從不執行任何任務。 –