Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Difference between join and terminate

Please take a look at the following code:

from multiprocessing import Process

def f(name):
    print 'hello', name

if __name__ == '__main__':
    p = Process(target=f, args=('bob',))
    p.start()
    p.join()

You will see that the function calls, start and join have been called here. Infact, they are always called in the examples of the multiprocessing module in the python documentation.

Now the reason why start is called so is fairly obvious, its because it starts the process. However, join is different from totally ending the process, as told in the documentation:

Block the calling thread until the process whose join() method is called terminates or until the optional timeout occurs.

So, from my understanding, join() is used to terminate the process. So why is not the terminate() function used in the examples of the documentation or TerminateProcess()?

And thus, that brings us to the main question, what is the difference between join and terminate? Ideally, what is join's purpose and what is terminate's purpose? Because they both seem be capable of doing the same thing according to the examples (correct me, if I'm mistaken).

I have so far discovered, that probably because terminate is different for both windows and linux, since windows has a different function for termination. Further reasons for the choice would also be appreciated.

like image 610
Games Brainiac Avatar asked Dec 15 '22 04:12

Games Brainiac


1 Answers

join is used to wait the process, not actively terminate the process, while terminate is used to kill the process.

Try following example (with / without p.terminate()):

from multiprocessing import Process
import time

def f(name):
    time.sleep(1)
    print 'hello', name

if __name__ == '__main__':
    p = Process(target=f, args=('bob',))
    p.start()
    p.terminate() # <---
    p.join()

With terminate, you get no output.

like image 166
falsetru Avatar answered Dec 24 '22 12:12

falsetru