August 2015
Intermediate to advanced
286 pages
5h 42m
English
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.
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 ...Read now
Unlock full access