I’m trying to stream a response to the client with the progress percentage after processing each file in the below code, however I see the error I/O operation on closed file
for each of the files.
The response received from server:
data: {"progress": 0}
data: {"error": "Error processing file: I/O operation on closed file."}
data: {"error": "Error processing file: I/O operation on closed file."}
data: {"progress": 100}
My example
@app.post("/test-streaming")
async def upload_file_streaming(files: list[UploadFile]):
return StreamingResponse(
stream_response(files),
media_type="text/event-stream"
)
async def extract(files) -> AsyncGenerator[str, None]:
total_files = len(files)
for index, file in enumerate(files):
try:
with pdfplumber.open(file.file) as pdf:
# Simulate processing each page of the PDF
for page in pdf.pages:
text = page.extract_text() # Replace with actual processing logic
await asyncio.sleep(0.5) # Simulate processing delay
# Calculate and yield progress
progress = int(((index + 1) / total_files) * 100)
yield f"data: {{"progress": {progress}}}nn"
except Exception as e:
yield f"data: {{"error": "Error processing file: {str(e)}"}}nn"
async def stream_response(files: list[UploadFile]) -> AsyncGenerator[str, None]:
yield "data: {"progress": 0}nn" # Initial progress
async for update in extract(files):
yield update
yield "data: {"progress": 100}nn"
I even tried reading the full file in memory by doing the below, however I see the same error
pdf_bytes = await file.read()
with pdfplumber.open(io.BytesIO(pdf_bytes)) as pdf:..
3