Python async request throwing error : object NoneType can't be used in 'await' expression

140 Views Asked by At

I am testing tornado websocket api with streamlit hello example application,

i added a async wrapper to tornado.websocket.WebSocketHandler get method to record websocket connections

# pip3 install streamlit
# streamlit already have inbuilt example hello
# you can start it by streamlit hello

#create a async wrapper
def async_wrapper(original):
    async def wrapper(*args, **kwargs):
        try:
            print("websocket connection")
            res = await original(*args,**kwargs)
            return res
        except Exception as exc:
            raise exc
    return wrapper

# copy paste the above code in tornado.websocket file and add decorator @async_wrapper to get method

For me the async wrapper is getting none object and it was throwing NonType object not awaitable error


2024-02-05 17:59:39.786 Uncaught exception GET /_stcore/stream (::1)
HTTPServerRequest(protocol='http', host='localhost:8501', method='GET', uri='/_stcore/stream', version='HTTP/1.1', remote_ip='::1')
Traceback (most recent call last):
  File "/env/lib/python3.10/site-packages/tornado/web.py", line 1790, in _execute
    result = await result
  File "wrapper.py", line 157, in wrapper
    raise exc
  File "wrapper.py", line 146, in wrapper
    res = await original(*args, **kwargs)
  File "/env/lib/python3.10/site-packages/tornado/websocket.py", line 273, in get
    await self.ws_connection.accept_connection(self)
  File "/env/lib/python3.10/site-packages/tornado/websocket.py", line 863, in accept_connection
    await self._accept_connection(handler)
  File "/env/lib/python3.10/site-packages/tornado/websocket.py", line 946, in _accept_connection
    await self._receive_frame_loop()
  File "/env/lib/python3.10/site-packages/tornado/websocket.py", line 1102, in _receive_frame_loop
    await self._receive_frame()
  File "/env/lib/python3.10/site-packages/tornado/websocket.py", line 1193, in _receive_frame
    await handled_future
TypeError: object NoneType can't be used in 'await' expression

Can anyone explain me what was the issue

Edit:

#Yes, it will work if we add a condition
method = original(*args, **kwargs)
#and check if method is coroutine and then use await
if method and inspect.iscoroutine(method):
   await method

My problem here is that some time the tornado websocket method is returring noneType object to await why it is like that

1

There are 1 best solutions below

1
Lajos Arpad On

Since it's not async, remove the await keyword:

original(*args,**kwargs)