close

[Solved] python-asyncio TypeError: object dict can’t be used in ‘await’ expression

Hello Guys, How are you all? Hope You all Are Fine. Today I get the following error python-asyncio TypeError: object dict can’t be used in ‘await’ expression in python. So Here I am Explain to you all the possible solutions here.

Without wasting your time, Let’s start This Article to Solve This Error.

How python-asyncio TypeError: object dict can’t be used in ‘await’ expression Error Occurs?

Today I get the following error python-asyncio TypeError: object dict can’t be used in ‘await’ expression in python.

How To Solve python-asyncio TypeError: object dict can’t be used in ‘await’ expression Error ?

  1. How To Solve python-asyncio TypeError: object dict can't be used in 'await' expression Error ?

    To Solve python-asyncio TypeError: object dict can't be used in 'await' expression Error Only asynchronous (defined with async def) functions can be awaited. Whole idea is that such functions are written special way what makes possible to run (await) them without blocking event loop.

  2. python-asyncio TypeError: object dict can't be used in 'await' expression

    To Solve python-asyncio TypeError: object dict can't be used in 'await' expression Error Only asynchronous (defined with async def) functions can be awaited. Whole idea is that such functions are written special way what makes possible to run (await) them without blocking event loop.

Solution 1

Only asynchronous (defined with async def) functions can be awaited. Whole idea is that such functions are written special way what makes possible to run (await) them without blocking event loop.

If you want to get result from common (defined with def) function that takes some considerable time to be executed you have these options:

  • rewrite this whole function to be asynchronous
  • call this function in another thread and await for result asynchronously
  • call this function in another process and await for result asynchronously

Usually you want to choose second option.

Here’s example of how to do it:

import asyncio
import time
from concurrent.futures import ThreadPoolExecutor


_executor = ThreadPoolExecutor(1)


def sync_blocking():
    time.sleep(2)


async def hello_world():
    # run blocking function in another thread,
    # and wait for it's result:
    await loop.run_in_executor(_executor, sync_blocking)


loop = asyncio.get_event_loop()
loop.run_until_complete(hello_world())
loop.close()

Solution 2

As thirdPartyAPIWrapper.data() is a normal sync function you should call it in another thread.

There is a helper function for that in a asgiref library.
Assume we’ve got a blocking function with an argument:

import asyncio
import time

from asgiref.sync import sync_to_async


def blocking_function(seconds: int) -> str:
    time.sleep(seconds)
    return f"Finished in {seconds} seconds"

async def main():
    seconds_to_sleep = 5
    function_message = await sync_to_async(blocking_function)(seconds_to_sleep)
    print(function_message)

loop = asyncio.get_event_loop()
loop.run_until_complete(main())
loop.close()

There is also an async_to_sync helper function in that library.

Summery

It’s all About this issue. Hope all solution helped you a lot. Comment below Your thoughts and your queries. Also, Comment below which solution worked for you? Thank You.

Also, Read