简体   繁体   中英

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

I want to write code that does something like-

In a thread call a function that will return two links and in the main thread keep printing "processing..." until that function, called in secondary thread returns those values

and when we get those return values the while loop of the main thread terminates and print those values.

Now I have tried writing few codes in python but couldn't manage to do it. I have just started python programming so I'm not familiar with it.

BTW the above-mentioned scenario is just a prototype.

The real case looks something like that:-

    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) 

In case further details are required then please just let me know.

Thank you for the help.

Tried a lot of things and now finally it got resolved.

Basically what I wanted to do was take two inputs from GUI then call a function (from another python program) with those two parameters and then once the processing is completed then from GUI user and either watch or download that content by pressing the watch or download buttons respectively.

So to do that earlier I was returning the watch and download link from that thread called function and even on calling that function on another thread, as it was returnig values after execution so the GUI freezes and shows not responding

Then after trying a lot of thing I came across daemon thing so I just made that thread daemon and that solved the main problem of freezing but now I wasn't able to take return values (when I tried to take the return values it again started to freeze the GUI)

So then I found an alternative to access those links from the main thread.

Here the point is if the function doesn't return anything that is being called in the thread then just make it daemon thread_name.daemon() = True and now it won't freeze the GUI

Now in case you wanna do something exactly after the thread is finished then this can be used thread_name.is_alive()

MY CODE LOOKS SOMETHING LIKE THAT:-

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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