Завершить процесс в питоне

Kill Process By PID in Python

You can kill a process via its process identifier, pid, via the os.kill() function.

In this tutorial you will discover how to kill a process via its pid.

Need To Kill a Process by PID

A process is a running instance of a computer program.

Every Python program is executed in a Process, which is a new instance of the Python interpreter. This process has the name MainProcess and has one thread used to execute the program instructions called the MainThread. Both processes and threads are created and managed by the underlying operating system.

Sometimes we may need to create new child processes in our program in order to execute code concurrently.

Python provides the ability to create and manage new processes via the multiprocessing.Process class.

You can learn more about multiprocessing in the tutorial:

In multiprocessing, we may need to kill a process by its process identifier or PID.

This may be for many reasons, such as:

  • The task executed by the process is no longer required.
  • The process has had an error or is out of control.
  • The main program is closing down due to a user request.

How can we kill a process via its pid in Python?

Run your loops using all CPUs, download my FREE book to learn how.

How To Kill a Process via its PID

You can kill a process via its pid with the os.kill() function.

The os.kill function takes two arguments, the process identifier (pid) to kill, and the signal to send to kill the process.

Читайте также:  Php модули на хостинге

The signal is a constant from the signal module, such as signal.SIGINT or signal.SIGKILL.

We need to know the pid for the process that is to be killed.

This can be retrieved from the multiprocessing.Process instance for the process via the pid attribute.

The multiprocessing.Process instance may be managed by the parent process when the child process is created, or accessed via a module function such as multiprocessing.active_children() or multiprocessing.parent_process().

You can learn more about getting the process pid in the tutorial:

The SIGINT or signal interrupt can be used to terminate the target process, which is equivalent to the user pressing CONTROL-C on the process. Alternately, the SIGKILL or signal kill process can be used to terminate the process forcefully.

The difference between SIGINT and SIGKILL is that it is possible for a process to detect and handle a SIGINT, whereas a SIGKILL cannot be handled.

Now that we know how to kill a process via pid, let’s look at some worked examples.

Confused by the multiprocessing module API?
Download my FREE PDF cheat sheet

Kill Current Process via PID

It is possible to kill the current process via pid.

This can be achieved by first getting the pid for the current process, then calling os.kill() with the pid and the signal to kill the process, such as SIGKILL.

First, we can get the pid for the current process using the os.getpid(), and report the result.

Источник

How to Terminate a running process on Windows in Python?

Process is a program that is being executed (processed). A process may not have to be one ran explicitly by the user, it could be a system process spawned by the operating system. Any applications that execute on an operating system firstly creates a process of its own to execute. In a typical os installation, most processes are os services and background applications, that are ran to maintain the operating system, software, and hardware. In this article, we will take a look at different ways of terminating running processes on a Windows OS, through python. Firstly we would describe a python method to achieve the result and then would look at a command found in Windows Command Processor for the equivalent effect.

Читайте также:  Html input all events

NOTE: This method is exclusive to Windows Operating systems. To achieve a similar effect on Linux, macOS refer to Kill command in Linux

Precautions

Terminating a concurrently running process should be done appropriately and with a conscience. As terminating a necessary process (ex. svhost, System, Windows Explorer, Antimalware Service Executable) could lead to inconsistency in operating system functionality, which could consequently lead to system crashes, blue screen of death, software malfunctioning, etc. Therefore it is generally advised to double-check the name of the applications/PID that is to be terminated beforehand.

Method 1:

Firstly, we would be using the wmi library for getting the list of the running process, and later would use this list to search for our desired process, and if found would terminate it. In order to install the module, execute the following command in the command interpreter of your operating system:

Python3

Explanation:

Firstly, we define a variable storing an integer value, which would serve to tell whether the process got terminated or not. This variable could also be used to determine how many processes under the same name have been terminated. Then, we specify the name of the process which we are willing to terminate. After which we initialize the WMI() class of wmi library. This allows us to use the methods found inside it such as WMI.Win32_Service, WMI.Win32_Process etc which are designed to perform different tasks. We would be using the WMI.Win32_Process function to get the list of running processes as wmi objects. Then we use the name attribute of the wmi object to the get name of the running process. After which we would be using primitive string matching to determine whether the name of the application matches the one specified before. If it does then we call the Terminate() method, to kill/terminate the process. After which we increment the value of ti, where the incremented value (or any non 0 value) would signify that at least one process has been terminated. After the end of the loop (when all the running processes names have been checked), we would check whether the value of variable ti is still 0 or not. If it is then no process got terminated, and we inform the user about the same using an Error message.

Читайте также:  Table row css display

Method 2:

Now we would be using an inbuilt utility found inside the windows command processor named WMIC (Windows Management Instrumentation Command-Line) to terminate the running process. The command we would be using :

wmic process where name="Process_Name" delete

Where Process_Name is the name of the process that we are terminating. To implement this utility in python we would be creating an instance of Windows Command line using os.system() and would be executing the command (bypassing it as argument) over there.
Code:

Источник

Оцените статью