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

4

26

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

Halona answered 3/7, 2019 at 14:27 Comment(0)
K
36

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.

Kubiak answered 8/7, 2020 at 10:13 Comment(0)
G
1

Flask and Flask-SocketIO do not work with asyncio. Either remove the asyncio stuff, or else drop Flask and Flask-SocketIO and use python-socketio, which does have support for asyncio.

Goy answered 3/7, 2019 at 22:6 Comment(4)
Thanks for the answer Miguel. Which server framework can i use with asyncio? I was using aiohttp and was facing frequent crashes.Halona
aiohttp is actually one of the best choices. There is also sanic and quart if you want to try something else.Goy
quart is a compatible replacement for flask.Toritorie
Also consider FastAPI.Jobye
M
0

To future Django coders. If you get this message in your Django Channels Consumer subclass, make sure the correct methods are marked as async:

    async def disconnect(self, close_code):
        pass

is the correct way. Without the prefix async, I get the same above error, whenever my client calls closes() the WS conn.

Millham answered 30/3 at 11:5 Comment(0)
S
0

The problem in your code is related to using the synchronous time.sleep(10) inside an asynchronous function. This causes an error because time.sleep() is not an asynchronous function and does not return an object that can be used with await. Instead, you need to use the asynchronous version of sleep from the asyncio module.

Saccharo answered 16/7 at 3:3 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.