kivy program guidance with multiple threads

45 views
Skip to first unread message

Charly

unread,
Aug 11, 2019, 12:31:41 PM8/11/19
to Kivy users support
Hi Folks,

I'm looking for guidance on Kivy programming when using multiple threads. I have written a Score Dashboard program in Kivy and I'm using TCP socket to receive update on the score screen. The display can have 100 entries, and each entry being composed bya BoxLayout, containing 5x BoxLayout, 30x Labels,. Upon reception of updates, my program can modify the score screen (updating text in Label), removing Label,Adding Label, Changing the order in the display...

While I'm very happy with the solution, I'm facing some "random" issues where either the program crash or the display update is not accurate. 

Bellow is an example of the crash.

 Traceback (most recent call last):

   File "main.py", line 195, in <module>

     ASYSClayScoresApp().run()

   File "/usr/local/lib/python2.7/dist-packages/kivy/app.py", line 826, in run

     runTouchApp()

   File "/usr/local/lib/python2.7/dist-packages/kivy/base.py", line 502, in runTouchApp

     EventLoop.window.mainloop()

   File "/usr/local/lib/python2.7/dist-packages/kivy/core/window/window_egl_rpi.py", line 92, in mainloop

     self._mainloop()

   File "/usr/local/lib/python2.7/dist-packages/kivy/core/window/window_egl_rpi.py", line 87, in _mainloop

     EventLoop.idle()

   File "/usr/local/lib/python2.7/dist-packages/kivy/base.py", line 346, in idle

     Clock.tick_draw()

   File "/usr/local/lib/python2.7/dist-packages/kivy/clock.py", line 588, in tick_draw

     self._process_events_before_frame()

   File "kivy/_clock.pyx", line 427, in kivy._clock.CyClockBase._process_events_before_frame

   File "kivy/_clock.pyx", line 467, in kivy._clock.CyClockBase._process_events_before_frame

   File "kivy/_clock.pyx", line 465, in kivy._clock.CyClockBase._process_events_before_frame

   File "kivy/_clock.pyx", line 167, in kivy._clock.ClockEvent.tick

   File "/usr/local/lib/python2.7/dist-packages/kivy/uix/boxlayout.py", line 299, in do_layout

     c = children[i]

 IndexError: list index out of range


I can't copy paste my program here as it's pretty large with multiple components.

I think both problems (crash or error in the display) occurs when I'm receiving lots of updates on the sockets, and updates' handler is changing the display structure while Kivy infrastructure is updating or drawing it at the same time. It looks to me a conflict due to 2 threads touching the same resource.

My question is : 
-> How can I know in my handler thread that Kivy infrastructure is idle ? or not performing a do_layout ?
-> Is there a primitive in the Kivy infrastructure I can query to ensure no action are currently perform on a given Widget ?

Thanks for your guidance,

Elliot Garbus

unread,
Aug 11, 2019, 1:32:31 PM8/11/19
to kivy-...@googlegroups.com

   File "/usr/local/lib/python2.7/dist-packages/kivy/uix/boxlayout.py", line 299, in do_layout

     c = children[i]

 IndexError: list index out of range


Looks like you are modifying the widget tree. 
How are you communicating between threads?  What is the high level thread architecture?
You might want to consider passing data to a queue, and processing that in the kivy event loop rather than having these updates occur simultaneously. 

Sent from my iPad
--
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/21eacb8e-6ed4-4fcb-b2c3-55e5ce75dd08%40googlegroups.com.

Charly

unread,
Aug 12, 2019, 4:13:18 AM8/12/19
to Kivy users support
Hi Elliot,

Thanks for your reply.  Yes, my handler can modify the widget tree. 
Overall architecture is that socket thread is putting my score events in a Queue, but that Queue is red from another thread which is independent from the kivy thread.
I don't know how to plug the execution of my handler in the Kivy thread. Do you have any pointer/documentation on how to perform this ?

Thanks,

To unsubscribe from this group and stop receiving emails from it, send an email to kivy-...@googlegroups.com.

Elliot Garbus

unread,
Aug 12, 2019, 9:54:28 AM8/12/19
to kivy-...@googlegroups.com

I’m  not certain how to do the integration, but here are some ideas.  I have not done any multi-threaded programming with kivy.

 

If you can make the traffic look like an HTTP request you could use the networking capability in Kivy:

https://kivy.org/doc/stable/api-kivy.network.urlrequest.html

 

Alternatively, if the thread that is putting messages in the queue, would increment a kivy numeric property, the change in the property, would cause an event to be fired in the next integration of the kivy event loop.

 

Another idea would be to schedule a kivy function to read the message queue.  At a regular interval, say 1 second or .1 second, this function would check the length of the queue, and pull off that number of messages.

 

Let me know what you think

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/ac3ecb22-bdce-40ef-b88f-759d11fb18f2%40googlegroups.com.

 

Charly

unread,
Aug 13, 2019, 3:10:14 AM8/13/19
to Kivy users support
Elliot,

I can't make easily the traffic looks like a HTTP request as it would require significant changes.
I would like to explore your second option, incrementing a kivy numeric property to trigger an event in the kivy event loop. 
I will keep you posted and provide feedback on this approach. 

Thanks for your guidance,

Elliot Garbus

unread,
Aug 13, 2019, 9:23:24 AM8/13/19
to kivy-...@googlegroups.com

I’m looking forward to hearing your progress.

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/d3291091-a3cf-45eb-b8ac-810eb50e87a9%40googlegroups.com.

 

Reply all
Reply to author
Forward
0 new messages