Alternative solution to Clock.schedule_interval

269 views
Skip to first unread message

Bostjan Parovel

unread,
Apr 8, 2021, 9:13:50 AM4/8/21
to Kivy users support
Hello fellow Kivy users!
I am checking a status of a GPIO pin on RPi4 using Clock.schedule_interval. When the main thread in Kivy is busy with e.g. a loop or popup, the checking is interrupted. Is there an alternative to Clock.schedule_interval?

Elliot Garbus

unread,
Apr 8, 2021, 11:00:43 AM4/8/21
to kivy-...@googlegroups.com

How frequently do you need to check the status of the pin?  Can you tolerate jitter?  The appropriate answer depends on your use case.

 

if you have long loops of execution, you could break them up into smaller pieces, If the method returns to the main kivy event loop it will check and run the methods that are scheduled.

Here are a few other solutions to consider:

  1. Select a different clock behavior: https://kivy.org/doc/stable/api-kivy.clock.html?highlight=clock#advanced-clock-details
  2. Use Threading, and check the state of the pin in a different thread.  Threading time shares execution, allowing concurrent execution of multiple threads.
  3. Use multi-processing if monitoring the pin requires a high resolution, low-jitter clock to check the state of the pin.  This puts the execution into a different process, on a multi core CPU, this will put the code on a different CPU core.  I used multiprocessing to create a low-latency low jitter clock (MIDI Clock) for a music related app I created.  Code here: https://github.com/ElliotGarbus/MidiClockGenerator  the readme captures some lessons learned.

 

I’m surprised to hear having a popup open is impacting the clock schedule.  I’m going to create a little test to see if that is true.

--
You received this message because you are subscribed to the Google Groups "Kivy users support" group.
To unsubscribe from this group and stop receiving emails from it, send an email to kivy-users+...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/kivy-users/ce96573c-47b8-48f4-8f08-7f7f2932c556n%40googlegroups.com.

 

Elliot Garbus

unread,
Apr 8, 2021, 11:16:07 AM4/8/21
to kivy-...@googlegroups.com

Here is a small test I put together.  I do not see the state of the Popup impacting the Clock.schedule_interval.

 

# Does having a popup open stop the Clock.schedule_interval method?

from kivy.app import App
from kivy.lang import Builder
from kivy.clock import Clock
from time import asctime

kv =
"""
#: import Factory kivy.factory.Factory
<TestPop@Popup>:
    title: 'Does Time Stop?'
    size_hint: .7,.7
    RelativeLayout:
        Button:
            text: 'Dismiss'
            on_release: root.dismiss()
            size_hint: None, None
            size: 100, 48
            pos_hint: {'center_x': 0.5, 'center_y': 0.5}

BoxLayout:
    Button:
        text: 'Open Popup'
        on_release: Factory.TestPop().open()


"""


class TestPopTimeApp(App):
   
def build(self):
       
return Builder.load_string(kv)

   
def on_start(self):
        Clock.schedule_interval(
self.print_time, 1)

   
def print_time(self, dt):
       
print(asctime())


TestPopTimeApp().run()

Bostjan Parovel

unread,
Apr 9, 2021, 1:49:16 PM4/9/21
to kivy-...@googlegroups.com
Hello Elliot,
Thank you for the reply. I am checking the pin every 30 ms in a process, it is a safety switch output. Kivy runs in a separate parallel process. To communicate the state of the pin I use a shared multiprocess value that Kivy reads.
Usiing threading to check the pin/shared value is however tricky. If I do the checking in a thread with @mainthread, it blocks the graphics. If I do the checking in a thread without @mainthread, the graphics interrupt the checking. I am stuck.
Boštjan Parovel


You received this message because you are subscribed to a topic in the Google Groups "Kivy users support" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/kivy-users/CsFJsXCm7DU/unsubscribe.
To unsubscribe from this group and all its topics, send an email to kivy-users+...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/kivy-users/606f1a96.1c69fb81.178d5.adfaSMTPIN_ADDED_MISSING%40gmr-mx.google.com.

Elliot Garbus

unread,
Apr 9, 2021, 3:06:10 PM4/9/21
to kivy-...@googlegroups.com

If already have another process reading the signal,  using schedule interval to read that signal every 100ms or perhaps 250ms should work fine.

Reply all
Reply to author
Forward
0 new messages