8 examples of 'how to terminate a program in python' in Python

Every line of 'how to terminate a program in python' code snippets is scanned for vulnerabilities by our powerful machine learning engine that combs millions of open source libraries, ensuring your Python code is secure.

All examples are scanned by Snyk Code

By copying the Snyk Code Snippets you agree to
this disclaimer
259def do_exit(self, line):
260 return self.exit()
Important

Use secure code every time

Secure your code as it's written. Use Snyk Code to scan source code in minutes – no build needed – and fix issues immediately. Enable Snyk Code

45def exit_program(key):
46 raise urwid.ExitMainLoop()
101def exit_program(ip_in_monitoring_dict: dict) -> None:
102
103 if len(ip_in_monitoring_dict):
104 for active_popen in ip_in_monitoring_dict.values():
105 active_popen.kill()
106 print("The program will be terminated in 5 seconds.\n")
107 time.sleep(4)
108 print("Bye...\n")
109 time.sleep(1)
110 sys.exit()
398def terminate(text="", rc=2):
399 if text:
400 errormsg(text)
401 sys.exit(rc)
255def _exit_function():
256 global _exiting
257
258 info('process shutting down')
259 debug('running all "atexit" finalizers with priority >= 0')
260 _run_finalizers(0)
261
262 for p in active_children():
263 if p._daemonic:
264 info('calling terminate() for daemon %s', p.name)
265 p._popen.terminate()
266
267 for p in active_children():
268 info('calling join() for process %s', p.name)
269 p.join()
270
271 debug('running the remaining "atexit" finalizers')
272 _run_finalizers()
16def terminate_main():
17 global main_proc
18
19 if main_proc:
20 while main_proc.is_alive():
21 main_proc.terminate()
22 time.sleep(1)
104def terminate(process):
105 """
106 Kills a process, useful on 2.5 where subprocess.Popens don't have a
107 terminate method.
108
109
110 Used here because we're stuck on 2.5 and don't have Popen.terminate
111 goodness.
112 """
113
114 def terminate_win(process):
115 import win32process
116 return win32process.TerminateProcess(process._handle, -1)
117
118 def terminate_nix(process):
119 import os
120 import signal
121 return os.kill(process.pid, signal.SIGTERM)
122
123 terminate_default = terminate_nix
124
125 handlers = {
126 "win32": terminate_win,
127 "linux2": terminate_nix
128 }
129
130 return handlers.get(sys.platform, terminate_default)(process)
125def terminate():
126 os.killpg(os.getpgid(0), signal.SIGTERM)

Related snippets