2013-07-23 52 views
0

我試圖運行同步操作演示here,預期的結果是:蟒蛇的條件等待和notify_all

Starting s1 
s1 done and ready for stage 2 
Starting stage_2[1] 
stage_2[1] running 
Starting stage_2[2] 
stage_2[2] running 

但有時我得到這個:

Starting stage_2[2] 
stage_2[2] running 
Starting stage_2[1] 
stage_2[1] running 
Starting s1 
s1 done and ready for stage 2 

我使用Python 2.7。 3和窗戶。任何人都知道爲什麼階段2s在stage1之前執行?

回答

0

從多處理文檔中可以看出,它是否採取了一些特殊措施來避免虛假喚醒,但通常在沒有條件循環的情況下調用cond.wait()會產生問題。

如果是這樣的問題,像這樣

def stage_2(cond): 
    """wait for the condition telling us stage_1 is done""" 
    name = multiprocessing.current_process().name 
    print 'Starting', name 
    with cond: 
     while stage < 2: 
      cond.wait() 
     print '%s running' % name 

會解決它。這裏stage實際上必須是一個用multiprocessing.Value()或類似的東西創建的ctypes對象。

+0

謝謝!這有幫助 – BullOnTheWay