簡體   English   中英

使用python的線程模塊

[英]Using python's Threading module

我正在開發一個 python 程序,該程序最終將能夠在 linux 系統上執行不同的操作(更新、互聯網速度測試、顯示機器信息......)其中一些操作需要時間,我的目標是顯示加載 Z6F1C25ED1523092F1BBF9DEEBE5Z任務被執行。 這需要執行異步函數和使用線程模塊。 我只是在程序的測試階段,遇到線程模塊的問題,不僅我的程序圖形界面的function在我執行其他動作時沒有執行,而且在執行結束時也沒有執行程序崩潰的輔助功能。 誰能幫我?

謝謝。

代碼(python文件):

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()

代碼(.kv 文件):

MainWidget:

<MainWidget>:
    Button:
        text: "MAJ"
        on_press: root.maj()
    AsyncImage:
        source: "images/test.gif"
        pos: 300, 300

使用www.DeepL.com/Translator翻譯(免費版)

不確定您要完成什么,但os.system()調用在新的 shell 中執行提供的命令(請參閱文檔)。 所以,當你執行:

th2 = threading.Thread(target=os.system("echo 'user_password' |sudo -S dnf -y upgrade"))

os.system()調用立即執行(不在新線程中),並且Thread創建嘗試將os.system()的返回安排為其目標。 然后th2.start()調用os.system()返回的任何內容(可能是退出代碼),並且應該產生錯誤。

無論如何,也許您應該使用subprocess.Popen()而不是Thread 請參閱文檔 這將在新進程中運行提供的系統命令,並且不會等待其完成。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM