1

I have 3 functions f1, f2, f3. I want to execute f1 and f2 in parallel and, once they are both completed, run f3. How could I achieve this?

from module1 import function1 as f1
from module2 import function2 as f2
from module3 import function3 as f3


if __name__ == '__main__':
    f1()
    f2()
    f3()
1
  • 2
    Take a look at the multiprocessing module. Commented Mar 25, 2022 at 20:31

1 Answer 1

1

You can use the multiprocessing module:

from multiprocessing import Process

from module1 import function1 as f1
from module2 import function2 as f2
from module3 import function3 as f3

def main():
    p1 = Process(target=f1)
    p2 = Process(target=f2)

    p1.start()
    p2.start()

    p1.join()
    p2.join()

    f3()

if __name__ == '__main__':
    main()
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks for the example! What is the difference between start and join?
Have a look at the documentation for a more precise answer but start() starts the process (on a new thread) and join() waits for the process to finish to continue the execution of the main thread

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.