kivy with asyncio

126 views
Skip to first unread message

Degenerate Tech

unread,
Jul 17, 2024, 11:38:16 AM7/17/24
to Kivy users support
is this right way to implement asyncio kivy  ? 
its block UI

from kivy.app import App
from kivy.uix.label import Label
import asyncio
import time
class ExampleApp(App):
   
    def build(self):
        return Label(text = 'Greetings Earthlings')

    def on_start(self):
        print("Kivy app started")
        asyncio.create_task(self.my_async_function())
   
    async def my_async_function(self):
        print("woohooo running async function on kivy app")
        result = await self.another_async_function()
        print(result)

    async def another_async_function(self):
        ## long task to get some data from cloud  
        a=[]
        for i in range(100000000):
            a.append(i)


        print("another async function")
        return a
   
       
# Start kivy app as an asynchronous task
asyncio.run(ExampleApp().async_run('asyncio'))

Juan Sanchez

unread,
Jul 17, 2024, 12:10:10 PM7/17/24
to kivy-...@googlegroups.com
You need to make sure it is not running on the main thread.  I think there are decorators for that that:

Also, if it is a long running operation, that is getting the global interpreter lock, you need to make sure that it yields to other threads from time to time.  If it is pure python, then it should just happen.  However, if it is in a C extension, you need to make sure that the GIL is released for operations that don't need it.


--
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/6837434b-9eec-4723-a30f-da1d7d0d5a9bn%40googlegroups.com.

ELLIOT GARBUS

unread,
Jul 17, 2024, 1:49:41 PM7/17/24
to kivy-...@googlegroups.com
There are a number of examples of using kivy with asyncio in the kivy-examples directory.
Open source UI framework written in Python, running on Windows, Linux, macOS, Android and iOS - kivy/kivy

FWIW I like to use Trio.

From: kivy-...@googlegroups.com <kivy-...@googlegroups.com> on behalf of Juan Sanchez <juan.e....@gmail.com>
Sent: Wednesday, July 17, 2024 9:09 AM
To: kivy-...@googlegroups.com <kivy-...@googlegroups.com>
Subject: Re: [kivy-users] kivy with asyncio
 
Reply all
Reply to author
Forward
0 new messages