簡體   English   中英

加入后Python管道子進程掛起

[英]Python piped subprocess hanging after join

希望這是一個相當容易回答的問題。 我正在嘗試在程序執行期間不斷向工作進程發送一組數據。 問題是,當我嘗試加入線程時,程序就會掛起。

我想也許工作人員終止了,我不需要join ,但是當我刪除join我的程序的電話時,最后會掛起。

這是我的代碼片段。 我試圖通過以這種方式使用工作來規避大型酸洗操作,因為我之前遇到過Queue對象的開銷問題。

# Outside of the class definition if that matters here...
def RunTimeWorker(conn, timestep, total_timesteps):
  print "Start worker", timestep, total_timesteps
  while (timestep < total_timesteps):
    data = conn.recv()
    timestep = data[0]
    print timestep, "DATA [" + str(data)
 conn.close()
 print "End worker"

並調用它的類方法:

def Execute(self):
  parent_conn, child_conn = Pipe()
  p = Process(target=RunTimeTestingWorker,args=(child_conn,0,300))
  p.start()

  for timestep in xrange(300):
    ... 
    # Send required data over to worker
    toProcessArr = [timestep,300,
       # trace data
       ...,...]
    parent_conn.send(toProcessArr)
    ...
  p.join # program hangs here

  #p.join  -- program will hang at end if join is commented

在這里,我的時間步驟正在成功更新......

Start worker 0 300
0 DATA [[0, 300, ...]
1 DATA [[1, 300, ...]
2 DATA [[2, 300, ...]
...
299 DATA [[299, 300, ...] # max timesteps are 300

編輯

大衛如此正確地指出,這對我來說是一個愚蠢的錯誤。 但是,他關於增加哨兵的評論非常有價值。

這是因為你的工作人員正在等待timestep < total_timesteps ,其中total_timesteps = 300timestep = 299 (因為timestepxrange(300) ,這是0..299)。

這里更好的模式是在處理完成后發送某種標記值。 例如,將worker更改為:

while True:
    data = con.recv()
    if data == "DONE":
        break

然后在制作人:

parent_conn.send("DONE")
p.join()

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM