Python как закрыть процесс

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.

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.

Источник

6 ways to exit program in Python

Exit Python program

Python is one of the most versatile and dynamic programming languages used out there. Nowadays, It is the most used programming language, and for good reason. Python gives a programmer the option and the allowance to exit a python program whenever he/she wants.

Using the quit() function

A simple and effective way to exit a program in Python is to use the in-built quit() function. No external libraries need to be imported to use the quit() function.

This function has a very simple syntax:

When the system comes up against the quit() function, it goes on and concludes the execution of the given program completely.

The quit() function can be used in a python program in the following way:

The Python interpreter encounters the quit() function after the for loop iterates once, and the program is then terminated after the first iteration.

Using the sys.exit() function

The sys module can be imported to the Python code and it provides various variables and functions that can be utilized to manipulate various pieces of the Python runtime environment.

The sys.exit() function is an in-built function contained inside the sys module and it is used to achieve the simple task of exiting the program.

It can be used at any point in time to come out of the execution process without having the need to worry about the effects it may have on a particular code.

The sys.exit() function can be used in a python program in the following way:

Using the exit() function

There exists an exit() function in python which is another alternative and it enables us to end program in Python.

It is preferable to use this in the interpreter only and is an alternative to the quit() function to make the code a little more user-friendly.

The exit() function can be used in a python program in the following way:

The two functions, exit() and quit() can only be implemented if and when the site module is imported to the python code. Therefore, these two functions cannot be used in the production and operational codes.

The sys.exit() method is the most popular and the most preferred method to terminate a program in Python.

Using the KeyboardInterrupt command

If Python program is running in cosole, then pressing CTRL + C on windows and CTRL + Z on unix will raise KeyboardInterrupt exception in the main thread.

If Python program does not catch the exception, then it will cause python program to exit. If you have except: for catching this exception, then it may prevent Python program to exit.

If KeyboardInterrupt does not work for you, then you can use SIGBREAK signal by pressing CTRL + PAUSE/BREAK on windows.

In Linux/Unix, you can find PID of Python process by following command:

and you can kill -9 to kill the python process. kill -9 will send SIGKILL and will stop the process immediately.

For example:
If PID of Python process is 6243, you can use following command:

In Windows, you can use taskkill command to end the windows process. YOu can also open task manager, find python.exe and end the process. It will exit Python program immediately.

Using the raise SystemExit command

Simply put, the raise keyword’s main function is to raise an exception. The kind of error you want to raise can be defined.

BaseException class is a base class of the SystemExit function. The SystemExit function is inherited from the BaseException such that it is able to avoid getting caught by the code that catches all exception.

The SystemExit function can be raised in the following way in Python code:

Источник

Читайте также:  Rock Paper Scissor
Оцените статью