DeprecationWarning: There is no current event loop 에러

글. 수알치 오상문 

 

Python 3.10.4에서 아래 예제를 테스트하면 제목에 있는 에러메시지가 나타난다. 그런데 실행은 잘 진행된다. 결론을 먼저 말하면, asynio 파이썬 버전 호환성 문제이며 코드는 정상적으로 실행된다. (참고: https://github.com/pytest-dev/pytest-asyncio/issues/212)

 

[소스 코드] asyncio.sleep()  테스트 코드 (출처: https://dojang.io/mod/page/view.php?id=2469) 

import asyncio
async def add(a, b):
    print('add: {0} + {1}'.format(a, b))
    await asyncio.sleep(1.0)  # 1초 대기 (asyncio.sleep도 네이티브 코루틴)
    return a + b    
async def print_add(a, b):
    result = await add(a, b)  # await로 다른 네이티브 코루틴 실행하고 반환값은 변수에 저장
    print('print_add: {0} + {1} = {2}'.format(a, b, result))
loop = asyncio.get_event_loop()           # 이벤트 루프를 얻음
loop.run_until_complete(print_add(1, 2))  # print_add 끝날 때까지 이벤트 루프 실행
loop.close()   # 이벤트 루프 종료

 

[실행 결과] Python 10 IDLE에서 실행한 결과 

Warning (from warnings module):
  File "C:/Users/USER/AppData/Local/Programs/Python/Python310/asyncio_delay_exam.py", line 12
    loop = asyncio.get_event_loop()             # 이벤트 루프를 얻음
DeprecationWarning: There is no current event loop
add: 1 + 2
print_add: 1 + 2 = 3

 

반응형

+ Recent posts