diff --git a/cosec_web/cosec_web.py b/cosec_web/cosec_web.py index 8ba0f0c..71b531e 100644 --- a/cosec_web/cosec_web.py +++ b/cosec_web/cosec_web.py @@ -38,6 +38,7 @@ sys.path.append("..") # For system-level activities: import os +import psutil # To work with date and time: import time @@ -201,6 +202,40 @@ class CosecWeb: self._printer.disable() self._nc_printer.disable() + # ┏┓ ┓┏┓•┓┓ + # ┃┃┏┓┏┓┏┏┓┏┏ ┃┫ ┓┃┃ + # ┣┛┛ ┗┛┗┗ ┛┛ ┛┗┛┗┗┗ + + @staticmethod + def kill_chrome_processes() -> int: + + """ + To kill previous Chrome-driver processes. Use this when you think that older processes are going to interfere + with new ones. + :return: The count of the processes that were killed. + """ + + kill_count = 0 + + # Enlist all the processes: + for proc in psutil.process_iter(['pid', 'name', 'cmdline']): + + # Try to kill the process: + try: + name = proc.info['name'] + cmd = proc.info['cmdline'] + if name and ('chrome' in name.lower() or 'chromedriver' in name.lower()): + # self._nc_printer("Killing Chrome Proc.", name, proc.pid, cmd) + proc.kill() + kill_count += 1 + + # In case of exceptions: + except (psutil.NoSuchProcess, psutil.AccessDenied): pass + + # Done here: + # self._nc_printer("Killed Chrome Procs.", kill_count) + return kill_count + # ┳┳┓• # ┃┃┃┓┏┏ # ┛ ┗┗┛┗