Guillaume Guillaume - 1 year ago 103
Python Question

python - How to avoid multiple running instances of the same function

In my python script I have a

core dumped
, and I think it's because the same function is called twice at the same time.

The function is the reading of a Vte terminal in a gtk window

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.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)

How to avoid the double executing of the function?

Answer Source

The double execution must be the consequence of the contents-changed event triggering twice.

You could just check in your term function whether it has already been executed before, and exit if so.

Add these two lines at the start of the term function:

    if hasattr(self, 'term_has_executed'): return
    self.term_has_executed = True
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download