2016-09-25 57 views
0

在我的Python腳本中我有一個core dumped,我認爲這是因爲同一個函數被同時調用兩次。蟒蛇 - 如何避免同一功能的多個運行實例

功能是一個VTE終端的在GTK窗口讀取

def term(self, dPluzz, donnees=None): 
    text = str(self.v.get_text(lambda *a: True).rstrip()) 
    [...] 
    print "Here is the time " + str(time.time()) 

def terminal(self):  
    self.v = vte.Terminal() 
    self.v.set_emulation('xterm') 
    self.v.connect ("child-exited", lambda term: self.verif(self, dPluzz)) 
    self.v.connect("contents-changed", self.term) 

結果:

Here is the time 1474816913.68 
Here is the time 1474816913.68 
Erreur de segmentation (core dumped) 

如何避免雙功能的執行?

+1

據我所知,唯一可能的途徑「雙執行」代碼段中使用線程。但是Python有Global Interpreter Lock,所以即使這樣它也不是真正的「雙重執行」。通常情況下,人們會將鎖添加到關鍵部分,以便線程在進入部分之前排隊。 https://docs.python.org/2/library/threading.html#lock-objects – Mai

+0

謝謝。雙面打印如何?奇怪的?這是我第一次看到它。該腳本在Ubuntu中運行良好,但該錯誤出現在OpenSuse中... – Guillaume

+0

[mcve]在這裏會受到歡迎。 –

回答

1

雙重執行必須是contents-changed事件觸發兩次的結果。

你可以在你的term函數中檢查它是否已經被執行過,如果是,就退出。

term功能開始加入這兩行:

if hasattr(self, 'term_has_executed'): return 
    self.term_has_executed = True 
+0

謝謝,但用這些行函數根本不執行。 說核心傾銷已經消失 – Guillaume

+0

編輯:我認爲它會工作無論如何。再次感謝:) – Guillaume

+0

不客氣;-) – trincot