Avoiding warning from GLib with GLib.source_remove()

Question:

When I need a part of my program that has GLib timeout functions set with

self.timeout_id = GLib.timeout_add_seconds(refresh, self._check_price)

I use

def stop(self):
    if self.timeout_id:
        GLib.source_remove(self.timeout_id)

to make sure this timeout_id still exists before attempting to remove it.

Yet I still get these pesky error messages from time to time:

Warning: Source ID 443 was not found when attempting to remove it
GLib.source_remove(self.timeout_id)

how do?

Asked By: bluppfisk

||

Answers:

The source is obviously getting removed via some other control path than stop(). The only candidate I can think of here (from the code you’ve provided) is the self._check_price method. If you’re returning False/GLib.SOURCE_REMOVE from that, you should also unset self.timeout_id:

def _check_price(self):
    …
    self.timeout_id = 0
    return GLib.SOURCE_REMOVE
Answered By: Philip Withnall

I think you can also use this :

def stop(self):
    if self.timeout_id:
        GLib.source_remove(self.timeout_id)
        self.timeout_id = None

Then the warning message disappears :

Warning: Source ID 350 was not found when attempting to remove it

Answered By: JEAN-PAUL T
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.