Function execution in parallel/concurrent

Viewed 31

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 Answers

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()
Related