Async/await of asyncio with nest_asyncio package is always returning coroutine 'get1' was never awaited

943 views Asked by At

Hii guys I am using asyncio with nest_asyncio but I am always getting coroutine never awaited

import asyncio
import tracemalloc
import aiohttp
import nest_asyncio
import json

tracemalloc.start()

async def request_call(url):
    async with aiohttp.ClientSession() as session:
        async with session.get(url) as response:
            assert response.status == 200
            data = await response.read()
            data = json.loads(data.decode("utf-8"))
            return data

def get_json(url):
    loop = asyncio.get_event_loop()
    nest_asyncio.apply(loop)
    result = loop.run_until_complete(request_call(url))
    return result

async def get2():
    url = "https://reqres.in/api/users?page=2"
    return get_json(url)

async def get1():
    return get2()

async def snap():
    return get1()

def data():
    result = asyncio.run(snap())
    print(result)

data()

Output :

<coroutine object get1 at 0x0471AD28> async.py:37: RuntimeWarning: coroutine 'get1' was never awaited Data() Object allocated at (most recent call last): File "async.py", lineno 31 return get1()

I cannot understand what is the problem and what is the fix?

Python=3.8.6 aiohttp=3.7.3 nest-asyncio=1.4.3

1

There are 1 answers

0
vibhor Gupta On

Just need to add await on certain function call

import asyncio
import tracemalloc
import aiohttp
import nest_asyncio
import json

tracemalloc.start()

async def request_call(url):
    async with aiohttp.ClientSession() as session:
        async with session.get(url) as response:
            assert response.status == 200
            data = await response.read()
            data = json.loads(data.decode("utf-8"))
            return data

def get_json(url):
    loop = asyncio.get_event_loop()
    nest_asyncio.apply(loop)
    result = loop.run_until_complete(request_call(url))
    return result

async def get2():
    url = "https://reqres.in/api/users?page=2"
    return get_json(url)

async def get1():
    return await get2()

async def snap():
    return await get1()

def data():
    result = asyncio.run(snap())
    print("AAAA")
    print(result)

data()