새소식

ETC/오류 해결 - 2023.02.28

asyncio의 RuntimeError: Event loop is closed 오류 해결 방법

  • -

원인

Python 3.8 이후 부터, 윈도우는 타 운영체제랑 다른 EventLoop를 기본 값으로 활용한다고 한다.

  • 타 OS 기본 : SelectorEventLoop
  • 윈도우 기본 : ProactorEventLoop

 

해결 방법

아래 코드를 입력해, 윈도우의 EventLoop를 SelectorEventLoop로 변경하면 된다.

asyncio.set_event_loop_policy(asyncio.WindowsSelectorEventLoopPolicy())
주의 사항

윈도우는 I/O Completion Ports를 활용해 비동기 처리 하기 때문에, SelectorEventLoop로 변경 시 아래와 같은 제약이 있다고 한다. 제약이 문제가 될 경우, Trio 라는 별도의 라이브러리를 사용하는 것이 좋다고 한다.

  • Can't support more than 512 sockets
  • Can't use pipe
  • Can't use subprocesses

참고

 

Asyncio event loop is closed when using asyncio.run()

I'm getting started to AsyncIO and AioHTTP, and i'm writing some basic code to get familiar with the syntax. I tried the following code that should perform 3 requests concurrently: import time import

stackoverflow.com

  • Asyncio의 문제와 해결법이 잘 설명돼있다.
 

Platform Support — Python 3.10.10 documentation

Platform Support The asyncio module is designed to be portable, but some platforms have subtle differences and limitations due to the platforms’ underlying architecture and capabilities. All Platforms Windows Source code: Lib/asyncio/proactor_events.py,

docs.python.org

  • 윈도우에서 asyncio를 사용할 때 제약 사항들이 나와있다.
Contents

포스팅 주소를 복사했습니다

이 글이 도움이 되었다면 공감 부탁드립니다.