How to exit the entire application from a Python thread?

Viewed 102808

How can I exit my entire Python application from one of its threads? sys.exit() only terminates the thread in which it is called, so that is no help.

I would not like to use an os.kill() solution, as this isn't very clean.

5 Answers

The easiest way to exit the whole program is, we should terminate the program by using the process id (pid).

import os
import psutil

current_system_pid = os.getpid()

ThisSystem = psutil.Process(current_system_pid)
ThisSystem.terminate()

To install psutl:- "pip install psutil"

For Linux you can use the kill() command and pass the current process' ID and the SIGINT signal to start the end steps to exit the app.

os.kill(os.getpid(), signal.SIGINT)
Related