[英]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在线程调用 function 将返回两个链接,并在主线程中继续打印“处理...”,直到在辅助线程中调用的 function 返回这些值
and when we get those return values the while loop of the main thread terminates and print those values.当我们得到这些返回值时,主线程的 while 循环终止并打印这些值。
Now I have tried writing few codes in python but couldn't manage to do it.现在我尝试在 python 中编写一些代码,但无法做到。 I have just started python programming so I'm not familiar with it.我刚开始 python 编程所以我不熟悉它。
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.基本上我想要做的是从 GUI 获取两个输入,然后使用这两个参数调用 function(来自另一个 python 程序),然后一旦处理完成,然后从 GUI 用户那里观看或通过按下手表或下载来下载该内容按钮。
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因此,为了做到这一点,我之前从名为 function 的线程返回了观看和下载链接,甚至在另一个线程上调用了 function,因为它是执行后的返回值,所以 GUI 冻结并显示没有响应
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)然后在尝试了很多事情之后我遇到了守护进程所以我只是做了那个线程守护进程并解决了冻结的主要问题但现在我无法获取返回值(当我尝试获取返回值时它再次开始冻结 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这里的重点是,如果 function 没有返回线程中正在调用的任何内容,那么只需使其成为守护进程thread_name.daemon() = True
现在它不会冻结 GUI
Now in case you wanna do something exactly after the thread is finished then this can be used thread_name.is_alive()
现在,如果您想在线程完成后立即执行某些操作,则可以使用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()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.