繁体   English   中英

在主线程中执行 while 循环,直到我们从辅助线程中调用的 function 获得返回值

[英]Executing a while loop in main thread until we get the return value from a function called in secondary thread

我想编写执行类似操作的代码 -

在线程调用 function 将返回两个链接,并在主线程中继续打印“处理...”,直到在辅助线程中调用的 function 返回这些值

当我们得到这些返回值时,主线程的 while 循环终止并打印这些值。

现在我尝试在 python 中编写一些代码,但无法做到。 我刚开始 python 编程所以我不熟悉它。

顺便说一句,上述场景只是一个原型。

真实案例看起来是这样的:-

    def search_button(self):              #main thread
        mname = self.root.ids.name.text
        quality = Quality
        
        #print(mname)
        #print(quality)

            
        global link4, link5

        with concurrent.futures.ThreadPoolExecutor() as executor:
            futures = executor.submit(movie_bot, mname, quality)    #movie_bot is function
            link4, link5 = futures.result()                         #returning two links



        while(link4==None and link5==None):
            self.root.ids.status.text = 'Searching, please wait...'
            self.root.ids.status.text = ''

        print(link4)
        print(link5) 

如果需要更多详细信息,请告诉我。

感谢您的帮助。

尝试了很多东西,现在终于解决了。

基本上我想要做的是从 GUI 获取两个输入,然后使用这两个参数调用 function(来自另一个 python 程序),然后一旦处理完成,然后从 GUI 用户那里观看或通过按下手表或下载来下载该内容按钮。

因此,为了做到这一点,我之前从名为 function 的线程返回了观看和下载链接,甚至在另一个线程上调用了 function,因为它是执行后的返回值,所以 GUI 冻结并显示没有响应

然后在尝试了很多事情之后我遇到了守护进程所以我只是做了那个线程守护进程并解决了冻结的主要问题但现在我无法获取返回值(当我尝试获取返回值时它再次开始冻结 GUI)

因此,我找到了从主线程访问这些链接的替代方法。

这里的重点是,如果 function 没有返回线程中正在调用的任何内容,那么只需使其成为守护进程thread_name.daemon() = True现在它不会冻结 GUI

现在,如果您想在线程完成后立即执行某些操作,则可以使用thread_name.is_alive()

我的代码看起来像这样:-

from selenium_l_headless import movie_bot
from kivy.lang import Builder 
from kivymd.app import MDApp
from  kivy.properties import ObjectProperty
from selenium_l_headless import *
import threading
import time
from kivy.clock import Clock


class MovieBot(MDApp):

    mname = ObjectProperty(None)
    quality = ObjectProperty(None)
    Quality = ObjectProperty(None)
    link4 = ObjectProperty(None)
    link5 = ObjectProperty(None)


    checks = []
    def build(self):
        self.theme_cls.theme_style= "Dark"
        self.theme_cls.primary_palette = "Teal" 
        return Builder.load_file('kivy_bot_md.kv')

        def checkBox_click(self, instance, value, Q):
            global Quality
            if value==True:
                MovieBot.checks.append(Q)
                Quality=''
                for x in MovieBot.checks:
                    Quality = f'{Quality}{x}'
            else:
                MovieBot.checks.remove(Q) 
                Quality = ''
                for x in MovieBot.checks:
                    Quality = f'{Quality} {x}'    
        def complete(self):
            self.root.ids.status.text = 'Searching completed, Now you can
                                         download or watch the movie!'
            global flag
            flag=0

        def status_sleep(self, *args):
        
            try:
                self.root.ids.status.text = 'Searching, please wait...'
            
                if(t1.is_alive() is False):
                    self.complete()
            except:
                pass

        
        def search_button(self):
            try:
                mname = self.root.ids.name.text
                quality = Quality

            
                global link4,link5
        
                global t1, flag
                flag=1
                t1 = threading.Thread(target = movie_bot, args= [mname, quality])
                t1.daemon = True
                t1.start()
        
                if(t1.is_alive()):
                    Clock.schedule_interval(self.status_sleep,1)
            except:
                pass

    def watch(self):
        try:
            if(flag is 1):
                pass
            else:
                self.root.ids.status.text = ''
                t2 = threading.Thread(target=watch_now)
                t2.daemon = True
                t2.start()
            except:
                pass

    def download(self):
        try:
            if(flag is 1):
                pass
            else:
                self.root.ids.status.text = ''
                t3 = threading.Thread(target=download_movie)
                t3.daemon = True
                t3.start()
        except:
            pass

    def close(self):
        exit(0) 


MovieBot().run()

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM