Kivy KivyMD - 如何使用多线程在 MDTabs 中填充不同的 MDgridLayouts

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

Python 3.6.4 - Kivy 1.11.1 - KivyMD 0.104.1

我对 Kivy 和 kivyMD 还很陌生,对线程和共享资源更是如此。

今天,我用SmartTileWithLabels依次填充了3个MDGridlayouts(sbgridfamily, sbgridgender, sbgridspecies)。它工作正常,但是加载时间很长(~5 秒)。

为了优化这个加载,我尝试了

  1. 将负载拆分到不同的线程中,而不使用锁或 信号量,一些单元格最终为空。

  2. 从结构中删除 MDGridlayouts 并在 在一个线程中飞行,然后我将第一个图块添加到 MDGridlayout.

    _apply_rule assert(rule not in self.rulectx)
    AssertionError
    

那么并行构建 MDGridlayout 的最佳方法是什么?

屏幕结构如下:

<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

Python 3.6.4 - Kivy 1.11.1 - KivyMD 0.104.1

我的问题是创建和加载大约 300 张缩略图以及 on-the-fly 调整图像大小,如果它们从缓存中丢失的话。

为了在不使用线程的情况下进行优化,我将在启动应用程序时使用 Clock.schedule_once 将我的下一个开发重定向到为每个选项卡至少加载一页(50 个缩略图),然后逐页加载需求,使用 on_scroll_start 和 on_scroll_move 滚动视图事件。

快速尝试后,使用此方法,第一次出现屏幕不到一秒,然后立即出现。