Error

[Error] io.UnsupportedOperation: read

nam-willow 2024. 12. 22. 01:40

오류

*** API error: POST: http://127.0.0.1:8080/주소 {'error': 
    (...중간생략)
    anyio.WouldBlock

    During handling of the above exception, another exception occurred:
    Traceback (most recent call last):
      File "/경로/경로/lib/python3.10/site-
      (...중간생략)
    anyio.EndOfStream

    During handling of the above exception, another exception occurred:
    Traceback (most recent call last):
      File "/경로/경로.py", line 567, in exception_handling
        return await call_next(request)
        (...중간생략)
      File "/경로/경로.py", line 3526, in upload_face_change
        original_image_data = await saved_file.read()
    io.UnsupportedOperation: read



원인

    async def 함수명(self, file: UploadFile, db: Session = Depends(get_db)): 

        # test 업로드한 파일 
        os.makedirs(f"생성할폴더", exist_ok=True)
        with open(f"읽어올 이미지파일", "rd") as  saved_file:
           original_image_data = await saved_file.read()


내가 이미지 파일을 테스트하려고 한건데 여기서 오류가 난거임→

  • open 함수로 파일을 열고, 이 파일 객체에 대해 비동기적인 read 연산을 시도하려고 했기 때문에 발생.
  • open 함수로 열린 파일 객체는 비동기 연산을 지원x. 따라서, original_image_data = await saved_file.read() 구문이 io.UnsupportedOperation: read 오류

anyio.WouldBlock

일반적으로 I/O 작업(파일 읽기/쓰기, 네트워크 요청 등)에서 비동기 처리를 잘못 설정했을 때 발생

anyio.EndOfStream

스트림의 끝에 도달. 예를 들어 파일 또는 네트워크 연결이 예상보다 일찍 닫혔거나 데이터가 손상된 경우 발생.

io.UnsupportedOperation: read

객체에서 read() 함수를 호출했으나, 해당 객체가 read 연산을 지원하지 않는 경우 발생.
이 오류는 특히 다음과 같은 상황에서 발생:

  • 비동기 작업에서 파일 핸들이 올바르게 열리지 않음.
  • saved_file이 파일 객체가 아닌 잘못된 데이터 형식으로 전달됨.
  • 파일 스트림이 이미 닫힌 상태에서 접근 시도.



해결

그냥 업로드된 이미지를 read()로 읽어오는걸로 변경

        file.file.seek(0) # 파일 포인터를 처음으로 이동
        original_image_data = await file.read()