Kivy KivyMD - How to use multithreading to fill up different MDgridLayouts within MDTabs

Viewed 425

Python 3.6.4 - Kivy 1.11.1 - KivyMD 0.104.1

I'm fairly new with Kivy and kivyMD, and even more with threading and shared resources.

Today, I fill in the 3 MDGridlayouts (sbgridfamily, sbgridgender, sbgridspecies) sequentially with SmartTileWithLabels. It works fine, however the loading time is quite long (~5 sec.).

To optimize this loading, I tried

  1. to split the loads in different threads, without using a lock or a semaphore, some cells end up empty.

  2. to remove the MDGridlayouts from the structure and create them on the fly in one thread, then I get an assertion error as soon as I add the first tile to a MDGridlayout.

    _apply_rule assert(rule not in self.rulectx)
    AssertionError
    

So what would be the best approach to build the MDGridlayouts in parallel?

The screen has the here below structure:

<Screen>
BoxLayout:
    orientation:'vertical'

    MDToolbar:
        title: 'Titre'
        ...
    
    MDTabs:

        Tab:
            text: "Famille"

            ScrollView:
                id: sbgfscrollview
                do_scroll_x: False

                MDGridLayout:
                    id: sbgridfamily
                    ...
        Tab:
            text: "Genre"

            ScrollView:
                do_scroll_x: False

                MDGridLayout:
                    id: sbgridgender
                    cols: 3
                    ... 

        Tab:
            text: "Esp\u00E8ce"

            ScrollView:
                do_scroll_x: False

                MDGridLayout:
                    id: sbgridspecies
                    cols: 4
                    ...

    # Will always be at the bottom of the screen.
    BottomAppBar:

Screen with first tab and gridlayout

1 Answers

Python 3.6.4 - Kivy 1.11.1 - KivyMD 0.104.1

My problem was the creation and loading time of about 300 thumbnails with on-the-fly resizing of the images, if they were missing from a cache.

In order to optimize, without using threads, I will redirect my next development towards loading galleries at least one page for each tab (50 thumbnails) using Clock.schedule_once when starting the application, then page by page on demand, using the on_scroll_start and on_scroll_move scrollview events.

After a quick try, with this approach, the screen appears in less than a second the first time, then it's immediate.

Related