我正在开发一款软件,其中涉及到调用API获取数据。然而,每当我调用API获取数据时,GUI界面就会冻结。起初我认为是线程问题,于是将函数转移到了线程中,但问题在于一旦使用join()函数,应用程序又会再次冻结。 现在,我希望能够在函数的同一个位置等待线程结束,然后从该函数的同一个位置继续执行下去。请问在Python中是否有实现这种功能的方法?
threads = []
def call_api(self, query, index, return_dict):
thread = threading.Thread( target=self.get_data, args=(query, index, return_dict))
self.threads.append(thread)
thread.start()
def get_all_tickets(self, platform):
if platform == 'All':
self.call_api(query1, 0, return_dict)
self.call_api(query2, 1, return_dict)
for thread in self.threads:
thread.join()
# The app freezes here
# Is there a way to wait at this point asynchronously until the processes are complete
and continue from that point without the GUI freezing?
join
使得像这样使用线程没有意义,因此您应该使用带有自定义信号的QThread。正如已经指出的那样,您正在尝试做什么还不清楚,而且您的代码也不足够。 - musicamantejoin()
并使用其他方法来检查是否获取到数据 - 您可以使用全局变量(或队列)并使用timer
定期执行函数,以检查您在全局变量(或队列)中拥有什么。或者使用 PyQt QThread,在获取所有数据时可能会有更好的执行此部分的方法。 - furas