[TROUBLESHOOTING] Runtimewarning: coroutine was never awaited

THOVY·2023년 2월 2일
1

TROUBLESHOOTING

목록 보기
33/41

코루틴이 기다리지 않습니다.

당황스럽게도 메서드가 실행되지 않았다.
상황은 이렇다.

ERROR ❌

과거의 나와 당신은 분명

def run(self):
	return asyncio.run(self.firstfunc())

이런 함수를 만들어놓고

async def coroutinemethod():
	print("wait a moment")
    
async def firstfunc(self):
    self.coroutinemethod()
	print("first funct)

이지랄해놓았을거다

Solution ✅

과거의 나와 당신은 분명 '아늬 함수를 호출했는데 왜? 뭘 안 기다린다는겨' 라고 했을 거지만,
코루틴 함수는 호출하면 함수를 실행하는 게 아니라 코루틴 객체를 생성한다.

firstfunc 메서드를

def firstfunc(self):
	❌ self.coroutinemethod()

def firstfunc(self):
	✅ temp = self.croutinemethod()

이렇게 하면 temp 객체를 사용하지 않더라도 일단 coroutinemethod 가 실행이 된다.

추가로 혼나기

물론 위의 솔루션은 맨 처음 실행하는 run 메서드를 asyncio 로 실행했을 때의 이야기다.
'아늬 객체 받아줬는데도 안 되는데요' 하면 미래의 자신에게 호되게 혼나야됨. 난 안 혼남✌
asyncio 로 실행하지 않은 거다.

난 한 번만 혼나서 매우 기분 좋음.

댕청해지지 않도록 항상 조심하자!

참고
https://superfastpython.com/asyncio-coroutine-was-never-awaited/

profile
BEAT A SHOTGUN

0개의 댓글