HumbleCoder
HumbleCoder

Reputation: 596

Kivy Clock unscheduling and rescheduling issue

I have a program that unschedule and reschedule clocks quite often. Then I found sometimes clock object stacks without being unscheduled.

Here is an example of what happens.

def functionA(self):
    if self.clock_variable is None:
        self.clock_variable = Clock.schedule_interval(self._function, 1)

...

def functionB(self):
    if self.clock_variable is not None:
        self.clock_variable.cancel()
        self.clock_variable = None

This sort of tasks, although it looks like it shouldn't, rarely cause a problem that clock object does not get removed and sleeps inside of self.clock_variable.

In Kivy, when assigning a clock variable, I can assign 2 or more clock objects in one variable like below.

variable = Clock.schedule_interval(func, 1) # First Clock
variable = Clock.schedule_interval(func, 2) # Second Clock

This doesn't cancel the first clock object and both clock objects remain. However, when I try to unschedule them. I can only unschedule the one I scheduled at last.

For example, the codes below would only unschedule the Second Clock. and I have no way to unschedule or remove the First Clock

variable.cancel()
variable.cancel() # Even if I cancel() twice, it works only once.

This is a huge problem because my system's CPU goes 20% to 90% when this occurs since clock object stacks inside a variable, and I have no way to remove them.

In Conclusion, I am looking for a way to limit a clock object to be assign only one by one to a variable. This problem only occurred when I loaded kivy program on Linux embedded system(with a single core low performance board).

Upvotes: 0

Views: 1508

Answers (1)

John Anderson
John Anderson

Reputation: 39012

If you only want one ClockEvent to exist at one time, then simply cancel the previous one before creating a new one:

def functionA(self):
    if self.clock_variable is None:
        self.clock_variable = Clock.schedule_interval(self._function, 1)
    else:
        self.clock_variable.cancel()
        self.clock_variable = Clock.schedule_interval(self._function, 1)

Upvotes: 2

Related Questions