TypeError: object NoneType can't be used in 'await' expression

daas-shukla picture daas-shukla · Jul 3, 2019 · Viewed 8.4k times · Source

function i am trying to call from my flask-socketio server

from flask_socketio import emit
import asyncio

async def myfunc():
     for i in range(10):
         j = 1*3
         await emit('update', {'j':j})

in my server function i am running

loop = asyncio.new_event_loop()
asyncio.set_event_loop(loop)
task = asyncio.gather(myfunc())
loop.run_until_complete(task)

I am getting an error on the 1st iteration of the loop one successful emit.

File "path\to\Python\Python37-32\Lib\threading.py", line 917, in _bootstrap_inner
    self.run()
  File "path\to\Python\Python37-32\Lib\threading.py", line 865, in run
    self._target(*self._args, **self._kwargs)
  File "path\to\lib\site-packages\socketio\server.py", line 636, in _handle_event_internal
    r = server._trigger_event(data[0], namespace, sid, *data[1:])
  File "path\to\lib\site-packages\socketio\server.py", line 665, in _trigger_event
    return self.handlers[namespace][event](*args)
  File "path\to\lib\site-packages\flask_socketio\__init__.py", line 280, in _handler
    *args)
  File "path\to\lib\site-packages\flask_socketio\__init__.py", line 694, in _handle_event
    ret = handler(*args)
  File "path\to\server.py", line 127, in print_message
    loop.run_until_complete(task)
  File "path\to\Python\Python37-32\Lib\asyncio\base_events.py", line 584, in run_until_complete
    return future.result()
  File "path\to\script.py", line 261, in fun
    await emit('update', {'j':j})
TypeError: object NoneType can't be used in 'await' expression

I want to be able to call myfunc() and emit an update to my socketio client on each iteration of for loop

Answer

rjdkolb picture rjdkolb · Jul 8, 2020

I also got this same error when I called await on a non async function.

e.g.

def do_something():
    print("Do Something")

async erroneous_function():
    await do_something()

The solution is simple, remove the await in front of the do_something() since it's not async.