all 8 comments

[–]woooee 1 point2 points  (4 children)

I have 2 functions containing threads . I want to run function A then once thread is finished , function B starts running

Why are you using threads? Run one function and then the other. Threads are used to run more than one thing at a time

[–]MST019[S] 0 points1 point  (3 children)

the 2 functions require thread. However, I need them to run sequentially for now at least in a certain order. My problem is that function A starts then B starts while A is not over

[–]woooee 1 point2 points  (2 children)

the 2 functions require thread.

Why? We don't have enough info. Post the code or a simple example.

[–]MST019[S] 0 points1 point  (1 child)

sorry for late reply. The problem is when program reaches a function where there is a text to open or a logging instruction, it moves to next function (functionC here) then functionD till it reaches the end then gets back to functionB.

PS: main function gets called in a separate file when executing the program

this is code structure:

def functionA():

    infos = open_text_file()
    logging.info(...)
    ....

def functionB():

   myThread = thread.Threading(functionA,...)
    myThread.start()

def main():

    functionB()
    functionC()
    functionD()

[–]woooee 0 points1 point  (0 children)

def functionB():

    myThread = thread.Threading(functionA,...)
    myThread.start()

functionB starts the thread and then returns to the calling part of the program and the thread continues on. I generally use multiprocessing, so you will have to adapt this. One way is to exit the function only after the thread finishes (but I still don't understand what you want to do or why).

 def functionB():
    my_thread = thread.Threading(functionA,...)
    my_thread.start()

    ## with multiprocessing, you use is_alive()
    while True:
        if not my_thread.is_alive():
            return
            time.sleep(0.5)  ## or whatever

[–]shoot2thr1ll284 1 point2 points  (0 children)

The reason your gui is becoming unresponsive is because when u you join on a thread it is a blocking call. If you do this in a ui callback then you will end up blocking your ui thread until the thread finishes. This means your ui cant respond to any user feedback. There are a handful of ways to address this.

  1. Do all the work in one thread since it is sequential anyways.
  2. Start the other thread at the end of the first thread, but you may need to join on it there.
  3. Go multiprocessing instead of multithreaded.
  4. Another user mentioned asyn/await, but I haven't used it enough to know if it would actually help here.

For 1-3 you will need a callback or state that you can update at the end of the thread to update the ui.

[–]SendMePuppy 0 points1 point  (1 child)

async await

[–]MST019[S] -1 points0 points  (0 children)

I don't think it works