I am developing a python program that will eventually be able to perform different actions on a linux system (updates, internet speed test, display of machine information...) Some of these actions take time and my goal is to display a loading animation while the task is performed. This requires the execution of asynchronous functions and the use of the threading module. I am only at the test phase of the program and I encounter a problem with the threading module, not only the function of the graphic interface of my program does not execute when I carry out the other actions but also at the end of the execution of the auxiliary functions the program crashes. Can anyone help me?
Thanks.
The code (python file):
from kivy.app import App
from kivy.uix.widget import Widget
import os
import threading
class MainWidget(Widget):
def __init__(self, **kwargs):
super().__init__(**kwargs)
def maj(self):
th2 = threading.Thread(target=os.system("echo 'user_password' |sudo -S dnf -y upgrade"))
th2.start()
class TestApp(App):
pass
th1 = threading.Thread(target=TestApp().run())
th1.start()
The code (.kv file):
MainWidget:
<MainWidget>:
Button:
text: "MAJ"
on_press: root.maj()
AsyncImage:
source: "images/test.gif"
pos: 300, 300
Translated with www.DeepL.com/Translator (free version)
CodePudding user response:
Not sure what you are trying to accomplish, but the os.system()
call executes the provided command in a new shell (See documentation). So, when you execute:
th2 = threading.Thread(target=os.system("echo 'user_password' |sudo -S dnf -y upgrade"))
The os.system()
call is executed immediately (not in a new thread), and the Thread
creation tries to schedule the return from the os.system()
as its target. Then the th2.start()
calls whatever the os.system()
had returned (probably an exit code), and should produce an error.
Anyway, perhaps you should be using subprocess.Popen()
instead of Thread
. See the documentation. This will run the provided system command in a new process and does not wait for its completion.