import psutil
import os
import logging
import time
# Configure logging
logging.basicConfig(level=logging.ERROR, format='%(asctime)s - %(levelname)s - %(message)s')
def teardown_datasets(dataset_names):
"""
Teardowns processes associated with specified datasets.
Args:
dataset_names (list): A list of dataset names to teardown.
"""
for dataset_name in dataset_names:
try:
# Find processes related to the dataset (you might need to adjust the filter)
for proc in psutil.process_iter(['pid', 'name', 'cmdline']):
if dataset_name in proc.info['cmdline']:
pid = proc.info['pid']
try:
# Attempt to terminate the process
proc.kill()
logging.info(f"Terminated process with PID {pid} associated with dataset {dataset_name}")
except psutil.NoSuchProcess:
logging.warning(f"Process with PID {pid} not found after attempting termination.")
except psutil.AccessDenied:
logging.error(f"Access denied to terminate process with PID {pid} associated with dataset {dataset_name}")
except Exception as e:
logging.error(f"Error terminating process with PID {pid} associated with dataset {dataset_name}: {e}")
except Exception as e:
logging.error(f"An unexpected error occurred while processing dataset {dataset_name}: {e}")
time.sleep(1) # Add a small delay to avoid overwhelming the system
if __name__ == '__main__':
# Example usage:
datasets_to_teardown = ["dataset_A", "dataset_B"] # Replace with your dataset names
teardown_datasets(datasets_to_teardown)
Add your comment