3

I am developing a software as part of my work, as soon as I make a call to the API to fetch data the GUI freezes, at first I understood the problem and transferred the functions to threads, the problem is that once I used the join() function the app froze again. What I would like is to wait at the same point in the function until the threads end and continue from the same point in the function, is there any way to do this in 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?
David_F
  • 33
  • 3
  • 1
    It appears you are mixing multiprocessing and threading, why ? – paisanco Jun 25 '22 at 13:57
  • Using `join` makes it pointless to use threading like this, so you should use a QThread with custom signals. As already pointed out, it's unclear what you're trying to do, and your code is also insufficient. – musicamante Jun 25 '22 at 14:02
  • First of all thank you very much for the quick response, and right I put some part of the code just to show the problem I have, can I get examples with QThread that gets a function and report when thread finished ? – David_F Jun 25 '22 at 14:13
  • remove `join()` and use other method to check if you get data - you could use global variables (or queue) and use `timer` to execute periodically function which check what you have in global variable (or in queue). OR use PyQt QThread which may have better method to execute this part when you get all data. – furas Jun 25 '22 at 18:18
  • Hi @furas , I would be happy if you can upload an example that described my case with QThread – David_F Jun 25 '22 at 21:32

1 Answers1

2

One possible option would be to use QThreads finished signal it emits that you can connect to with a slot that contains the remaining logic from your get_all_tickets method.

threads = []
def call_api(self, query, index, return_dict):
    thread = QThread() 
    worker = Worker(query, index, return_dict)
    worker.moveToThread(thread)
    thread.started.connect(worker.run)
    worker.finished.connect(thread.terminate)
    thread.finished.connect(self.continue_getting_all_tickets)    
    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)

def continue_getting_all_tickets(self):
    # this will be called once for each and every thread created
    # if you only want it to run once all the threads have completed 
    # you could do something like this:
    if all([thread.isFinished() for thread in self.threads]):
        # ... do something
    

The worker class could look something like this.

class Worker(QObject):
    finished = Signal()
    def __init__(self, query, index, return_dict):
        super().__init__()
        self.query = query
        self.index = index
        self.return_dict = return_dict

    def run(self):
        # this is where you would put `get_data` code
        # once it is finished it should emit the finished signal
        self.finished.emit()      

Hopefully this will help you find the right direction.

Alexander
  • 16,091
  • 5
  • 13
  • 29
  • Hi @alexpdev , i attached a post under your answer , thanks – David_F Jun 25 '22 at 21:33
  • thanks for your response @alexpdev , you can please help me with the class that the QThread should get ? – David_F Jun 25 '22 at 21:59
  • @David_F Please see more detailed answer. If you still need assisstance then you will need to include more of your code – Alexander Jun 25 '22 at 22:30
  • thanks you very much for your help i added another function that check the status of the thread and i fixed the following code that you suggest: you: if all([thread.isFinished() for thread in self.threads]): me: if all([not thread.is_alive() for thread in self.threads]): – David_F Jun 26 '22 at 07:41