1. import psutil
  2. import os
  3. import logging
  4. import time
  5. # Configure logging
  6. logging.basicConfig(level=logging.ERROR, format='%(asctime)s - %(levelname)s - %(message)s')
  7. def teardown_datasets(dataset_names):
  8. """
  9. Teardowns processes associated with specified datasets.
  10. Args:
  11. dataset_names (list): A list of dataset names to teardown.
  12. """
  13. for dataset_name in dataset_names:
  14. try:
  15. # Find processes related to the dataset (you might need to adjust the filter)
  16. for proc in psutil.process_iter(['pid', 'name', 'cmdline']):
  17. if dataset_name in proc.info['cmdline']:
  18. pid = proc.info['pid']
  19. try:
  20. # Attempt to terminate the process
  21. proc.kill()
  22. logging.info(f"Terminated process with PID {pid} associated with dataset {dataset_name}")
  23. except psutil.NoSuchProcess:
  24. logging.warning(f"Process with PID {pid} not found after attempting termination.")
  25. except psutil.AccessDenied:
  26. logging.error(f"Access denied to terminate process with PID {pid} associated with dataset {dataset_name}")
  27. except Exception as e:
  28. logging.error(f"Error terminating process with PID {pid} associated with dataset {dataset_name}: {e}")
  29. except Exception as e:
  30. logging.error(f"An unexpected error occurred while processing dataset {dataset_name}: {e}")
  31. time.sleep(1) # Add a small delay to avoid overwhelming the system
  32. if __name__ == '__main__':
  33. # Example usage:
  34. datasets_to_teardown = ["dataset_A", "dataset_B"] # Replace with your dataset names
  35. teardown_datasets(datasets_to_teardown)

Add your comment