【问题标题】:Executing a while loop in main thread until we get the return value from a function called in secondary thread在主线程中执行一个while循环,直到我们从辅助线程中调用的函数获得返回值
【发布时间】:2021-05-18 11:18:24
【问题描述】:

我想写一些类似的代码-

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

当我们得到这些返回值时,主线程的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) 

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

感谢您的帮助。

【问题讨论】:

    标签: python multithreading selenium kivymd


    【解决方案1】:

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

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

    为此,我早些时候从那个调用函数的线程返回了监视和下载链接,甚至在另一个线程上调用该函数,因为它是执行后返回的值,所以 GUI 冻结并显示 没有响应

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

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

    这里的重点是,如果函数不返回线程中正在调用的任何内容,那么只需将其设为守护进程 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()
    

    【讨论】:

      猜你喜欢
      • 2022-01-05
      • 2011-05-22
      • 1970-01-01
      • 1970-01-01
      • 2013-01-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多