process.kill method

The process.kill() method of Python kills the process associated with the process instance. It used SIGKILL to terminate the process.

Process Updated 6 Feb 2019 1 min read

Method Name

kill

Method Signature

kill()

Return Value

NoneType

Parameters

Not Applicable

Method Overview

  • When invoked kill() uses SIGKILL and terminates the process.
  • When SIGKILL is used, the process does not know it has been issued a SIGKILL. The process cannot do any cleanup. The process is killed immediately.Remember, though miniscule it takes a finite time to kill the process.

Example

python
import multiprocessing as multiproc

import time

def processFunction(mq):

    for i in range(1, 10, 2):

        msg = "Child:Message"

        mq.put(msg)

        print(msg)

        time.sleep(1)

if __name__ == "__main__":

    multiproc.set_start_method('fork')

    mq = multiproc.Queue()

    childProcess = multiproc.Process(target=processFunction, args=(mq,))

    childProcess.start()

    childProcess.kill()

    # It takes a while to completely terminate the process using SIGKILL

    print("Child process is alive: %s "%childProcess.is_alive());

    print("Child process is alive: %s "%childProcess.is_alive());

    print("Child process is alive: %s "%childProcess.is_alive());

    print("Child process is alive: %s "%childProcess.is_alive());

    print("Child process is alive: %s "%childProcess.is_alive());

    print("Child process is alive: %s "%childProcess.is_alive());

    while mq.empty() is False:

        print(mq.get()) # Bad practice

Output

Output
Child process is alive: True

Child process is alive: False

Child process is alive: False

Child process is alive: False

Child process is alive: False

Child process is alive: False