How to kill a process
It's possible to kill a process immediately using the terminate() method. Also, we use the is_alive() method to keep track of whether the process is alive or not.
How to do it...
In this example, a process is created with the target function foo(). After the start, we kill it with the terminate() function:
#kill a Process: Chapter 3: Process Based Parallelism
import multiprocessing
import time
def foo():
print ('Starting function')
time.sleep(0.1)
print ('Finished function')
if __name__ == '__main__':
p = multiprocessing.Process(target=foo)
print ('Process before execution:', p, p.is_alive())
p.start()
print ('Process running:', p, p.is_alive())
p.terminate()
print ('Process terminated:', p, p.is_alive())
p.join()
print ('Process joined:', p, p.is_alive())
print ('Process exit code:', p.exitcode)The following is the output we get when...