import os
import time
import aiofiles
async def process_file(filepath):
"""Processes a single file and releases resources."""
try:
async with aiofiles.open(filepath, 'r') as f:
# Perform processing on the file content
content = await f.read()
# Simulate processing time
await asyncio.sleep(0.1)
print(f"Processed: {filepath}")
return True # Indicate successful processing
except Exception as e:
print(f"Error processing {filepath}: {e}")
return False
async def batch_process_files(file_list, max_retries=3, retry_delay=5):
"""Processes a list of files with retry logic."""
successful_count = 0
for filepath in file_list:
retries = 0
while retries < max_retries:
try:
success = await process_file(filepath)
if success:
successful_count += 1
break # Exit retry loop if successful
except Exception as e:
print(f"Retry attempt {retries + 1} failed for {filepath}: {e}")
retries += 1
if retries < max_retries:
await asyncio.sleep(retry_delay)
else:
pass #If no exception, break the inner loop
else:
print(f"Failed to process {filepath} after {max_retries} retries.")
async def main():
"""Example usage with a list of files."""
files = ["file1.txt", "file2.txt", "file3.txt"] # Replace with your file list
# Create dummy files for testing
for file in files:
with open(file, "w") as f:
f.write(f"This is file {file}")
await batch_process_files(files)
#Clean up dummy files
for file in files:
os.remove(file)
if __name__ == "__main__":
import asyncio
asyncio.run(main())
Add your comment