pythonpython-3.xkivykivymdbuildozer

Kivy Buildozer: aiohttp asyncio Issue, App Crashes


I will try to expain my problem exactly. First of all, my kivy app and its all sections works in VScode. It is a calculator app that scrape some datas and calculate them

Python version: 3.11.7 Kivy version: 2.3.0

I have two functions in my code so i want them to work together at the same time so i used aiohttp asyncio. Lets start

Libraries that i used in my app

from kivy.app import App
from kivy.uix.boxlayout import BoxLayout
from kivy.uix.label import Label
from kivy.uix.image import Image
from kivy.uix.behaviors import ButtonBehavior
from kivy.metrics import dp
from kivy.clock import mainthread
from kivy.uix.popup import Popup
from kivy.factory import Factory
from kivy.properties import ObjectProperty
from time import sleep
from unidecode import unidecode
from bs4 import BeautifulSoup
import aiohttp
import asyncio
import requests
import threading
import re 

Code example of aiohttp / asyncio

@mainthread
    def calculate(self, lig, *args):


        async def fetch_stats_home(session, url, match_codes_home):
            try:
                querystring = {}
                headers = {}

                async with session.get(url, params=querystring, headers=headers) as response:
                    
                    soup = BeautifulSoup(await response.text(), "html.parser")

                
           
        async def fetch_stats_away(session, url, match_codes_away):

            try:
                querystring = {}
                headers = {}

                async with session.get(url, params=querystring, headers=headers) as response:

                    soup = BeautifulSoup(await response.text(), "html.parser")                  

            except:
                pass         

        async def main():
            url = "https://Handler.aspx"    
            match_codes_home = evsahibi_evindeki_maclar_kodlar
            match_codes_away = deplasman_deplasmandki_maclar_kodlar  

            async with aiohttp.ClientSession() as session:
                tasks_home  = [fetch_stats_home(session, url, match_code) for match_code in match_codes_home]
                tasks_away  = [fetch_stats_away(session, url, match_code) for match_code in match_codes_away]
                await asyncio.gather(*tasks_home, *tasks_away)

        asyncio.run(main())

VSCODE TESTS & Terminal Logs (APP Works Without a Problem)

[DEBUG  ] [Using proactor] IocpProactor
[DEBUG  ] [Starting new HTTPS connection (1)] .com:443
[DEBUG  ] [https          ]//.com:443 "GET /Takim/451/ HTTP/1.1" 200 27720
[DEBUG  ] [https          ]//.com:443 "GET /Takim/570/ HTTP/1.1" 200 28575
[DEBUG  ] [Using proactor] IocpProactor
[DEBUG  ] [Starting new HTTPS connection (1)] arsiv.mackolik.com:443
[DEBUG  ] [https          ]//.com:443 "GET /Takim/3/ HTTP/1.1" 200 29920
[DEBUG  ] [https          ]//:443 "GET /Takim/447/ HTTP/1.1" 200 28574
[DEBUG  ] [Using proactor] IocpProactor
[DEBUG  ] [Starting new HTTPS connection (1)] arsiv.mackolik.com:443
[DEBUG  ] [https          ]//:443 "GET /Takim/8/ HTTP/1.1" 200 28140
[DEBUG  ] [https          ]//:443 "GET /Takim/448/ HTTP/1.1" 200 28235
[DEBUG  ] [Using proactor] IocpProactor
Scroll 1
APP WORKS

BUILDOZER: i compiled app in Buildozer, when i click Calculate button then APP crashes.

Here is the log : adb logcat -s python

As you can see, at the first [Using proactor] IocpProactor , APP Crashes.

Starting new HTTPS connection (1)] arsiv.mackolik.com:443

02-17 13:59:55.087 12778 13111 I python  : [DEBUG  ] [https          ]//arsiv.mackolik.com:443 "GET /Takim/574/ HTTP/1.1" 200 27720
02-17 13:59:56.036 12778 13111 I python  : [DEBUG  ] [https          ]//arsiv.mackolik.com:443 "GET /Takim/446/ HTTP/1.1" 200 28575
02-17 13:59:56.346 12778 13111 I python  : [DEBUG  ] [Using selector] EpollSelector

02-17 13:59:56.762 12778 13111 I python  : [INFO   ] [Base        ] Leaving application in progress...
02-17 13:59:56.763 12778 13111 I python  :  Traceback (most recent call last):
02-17 13:59:56.763 12778 13111 I python  :    File "/home/seo/Desktop/APP/.buildozer/android/app/main.py", line 825, in <module>
02-17 13:59:56.763 12778 13111 I python  :    File "/home/seo/Desktop/APP/.buildozer/android/platform/build-arm64-v8a_armeabi-v7a/build/python-installs/footballpredictor/arm64-v8a/kivy/app.py", line 956, in run
02-17 13:59:56.763 12778 13111 I python  :    File "/home/seo/Desktop/APP/.buildozer/android/platform/build-arm64-v8a_armeabi-v7a/build/python-installs/footballpredictor/arm64-v8a/kivy/base.py", line 574, in runTouchApp
02-17 13:59:56.764 12778 13111 I python  :    File "/home/seo/Desktop/APP/.buildozer/android/platform/build-arm64-v8a_armeabi-v7a/build/python-installs/footballpredictor/arm64-v8a/kivy/base.py", line 339, in mainloop
02-17 13:59:56.764 12778 13111 I python  :    File "/home/seo/Desktop/APP/.buildozer/android/platform/build-arm64-v8a_armeabi-v7a/build/python-installs/footballpredictor/arm64-v8a/kivy/base.py", line 379, in idle
02-17 13:59:56.764 12778 13111 I python  :    File "/home/seo/Desktop/APP/.buildozer/android/platform/build-arm64-v8a_armeabi-v7a/build/python-installs/footballpredictor/arm64-v8a/kivy/clock.py", line 733, in tick
02-17 13:59:56.765 12778 13111 I python  :    File "/home/seo/Desktop/APP/.buildozer/android/platform/build-arm64-v8a_armeabi-v7a/build/python-installs/footballpredictor/arm64-v8a/kivy/clock.py", line 776, in post_idle
02-17 13:59:56.765 12778 13111 I python  :    File "kivy/_clock.pyx", line 620, in kivy._clock.CyClockBase._process_events
02-17 13:59:56.765 12778 13111 I python  :    File "kivy/_clock.pyx", line 653, in kivy._clock.CyClockBase._process_events
02-17 13:59:56.765 12778 13111 I python  :    File "kivy/_clock.pyx", line 649, in kivy._clock.CyClockBase._process_events
02-17 13:59:56.766 12778 13111 I python  :    File "kivy/_clock.pyx", line 218, in kivy._clock.ClockEvent.tick
02-17 13:59:56.766 12778 13111 I python  :    File "/home/seo/Desktop/APP/.buildozer/android/platform/build-arm64-v8a_armeabi-v7a/build/python-installs/footballpredictor/arm64-v8a/kivy/clock.py", line 1095, in callback_func
02-17 13:59:56.766 12778 13111 I python  :    File "/home/seo/Desktop/APP/.buildozer/android/app/main.py", line 598, in calculate
02-17 13:59:56.766 12778 13111 I python  :  ZeroDivisionError: division by zero
02-17 13:59:56.766 12778 13111 I python  : Python for android ended.

Buildozer.spec

requirements = python3==3.11.7,kivy==2.3.0,aiohttp==3.9.3,aiosignal==1.3.1,attrs==23.2.0,beautifulsoup4==4.12.3,certifi==2024.2.2,charset-normalizer==3.3.2,docutils==0.20.1,frozenlist==1.4.1,idna==3.6,multidict==6.0.5,requests==2.31.0,soupsieve==2.5,Unidecode==1.3.8,urllib3==2.2.0,yarl==1.9.4

osx.python_version = 3.11.7
osx.kivy_version = 2.3.0

UPDATE Found Problem but can't fixed

When i create APK via buildozer, i checked with "buildozer android debug deploy run logcat" that 'async with session.get(url, params=querystring, headers=headers) as response:' sections do not work and also beautifulsoup which is inside does not work as well so data is not scraped! and we get ZeroDivisionError

But in VSCODE there are no problem.

Why 'async with session.get(url, params=querystring, headers=headers) as response:' part does not work in kivy?

Thanks


Solution

  • First of all i checked the error at async with session.get(url, params=querystring, headers=headers) as response: because it does not execute

    try:
        async with session.get(url, params=querystring, headers=headers) as response:
            print("mecra")  # Add this line
            soup = BeautifulSoup(await response.text(), "html.parser")
    except Exception as e:
        print(f"Exception: {e}")
    

    the error was:

    Cannot connect to host .com:443 ssl:True [SSLCertVerificationError: (1, '[SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed:

    I get help from chatgpt:

    Now i import ssl to python

    new code

    ssl_context = ssl.create_default_context()
    ssl_context.check_hostname = False
    ssl_context.verify_mode = ssl.CERT_NONE
    async with session.get(url, params=querystring, headers=headers, ssl=ssl_context) as response:
    

    now async with session.get(url, params=querystring, headers=headers) as response: execute and my code works :)