1. import time
  2. import tempfile
  3. import shutil
  4. import os
  5. import threading
  6. def cleanup_artifacts(func, timeout, *args, **kwargs):
  7. """
  8. Executes a function with a timeout and cleans up temporary files
  9. created during execution.
  10. Args:
  11. func: The function to execute.
  12. timeout: The maximum execution time in seconds.
  13. *args: Positional arguments for the function.
  14. **kwargs: Keyword arguments for the function.
  15. Returns:
  16. The return value of the function if it completes within the timeout,
  17. otherwise None. Also ensures cleanup.
  18. """
  19. results = {} # Store results from the function
  20. exception = None # Store any exception raised by the function
  21. def execute_with_timeout():
  22. try:
  23. results = func(*args, **kwargs)
  24. except Exception as e:
  25. exception = e
  26. def cleanup():
  27. try:
  28. # Attempt to remove temporary files/directories
  29. for path in os.listdir(tempfile.gettempdir()):
  30. full_path = os.path.join(tempfile.gettempdir(), path)
  31. if os.path.isdir(full_path):
  32. shutil.rmtree(full_path) # Remove directories recursively
  33. else:
  34. os.remove(full_path) # Remove files
  35. except Exception as e:
  36. print(f"Error during cleanup: {e}")
  37. def timed_execution():
  38. start_time = time.time()
  39. try:
  40. execute_with_timeout()
  41. except Exception as e:
  42. exception = e
  43. finally:
  44. elapsed_time = time.time() - start_time
  45. if elapsed_time > timeout:
  46. print(f"Function timed out after {timeout} seconds.")
  47. raise TimeoutError(f"Function execution exceeded timeout ({timeout} seconds)")
  48. thread = threading.Thread(target=timed_execution)
  49. thread.daemon = True # Allow the main thread to exit even if this thread is running
  50. thread.start()
  51. thread.join(timeout) # Wait for the thread to finish (with a timeout)
  52. if exception:
  53. print(f"Function execution failed: {exception}")
  54. return None # Indicate failure
  55. else:
  56. return results # Return the result if successful

Add your comment