1. import os
  2. import time
  3. import aiofiles
  4. async def process_file(filepath):
  5. """Processes a single file and releases resources."""
  6. try:
  7. async with aiofiles.open(filepath, 'r') as f:
  8. # Perform processing on the file content
  9. content = await f.read()
  10. # Simulate processing time
  11. await asyncio.sleep(0.1)
  12. print(f"Processed: {filepath}")
  13. return True # Indicate successful processing
  14. except Exception as e:
  15. print(f"Error processing {filepath}: {e}")
  16. return False
  17. async def batch_process_files(file_list, max_retries=3, retry_delay=5):
  18. """Processes a list of files with retry logic."""
  19. successful_count = 0
  20. for filepath in file_list:
  21. retries = 0
  22. while retries < max_retries:
  23. try:
  24. success = await process_file(filepath)
  25. if success:
  26. successful_count += 1
  27. break # Exit retry loop if successful
  28. except Exception as e:
  29. print(f"Retry attempt {retries + 1} failed for {filepath}: {e}")
  30. retries += 1
  31. if retries < max_retries:
  32. await asyncio.sleep(retry_delay)
  33. else:
  34. pass #If no exception, break the inner loop
  35. else:
  36. print(f"Failed to process {filepath} after {max_retries} retries.")
  37. async def main():
  38. """Example usage with a list of files."""
  39. files = ["file1.txt", "file2.txt", "file3.txt"] # Replace with your file list
  40. # Create dummy files for testing
  41. for file in files:
  42. with open(file, "w") as f:
  43. f.write(f"This is file {file}")
  44. await batch_process_files(files)
  45. #Clean up dummy files
  46. for file in files:
  47. os.remove(file)
  48. if __name__ == "__main__":
  49. import asyncio
  50. asyncio.run(main())

Add your comment