O'Reilly logo

Python Parallel Programming Cookbook by Giancarlo Zaccone

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

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 ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required