[英]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.