python-asyncio - 类型错误 : object NoneType can't be used in 'await' expression

标签 python-asyncio flask-socketio

我试图从我的 Flask-socketio 服务器调用的函数

from flask_socketio import emit
import asyncio

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


在我的服务器功能中,我正在运行

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

我收到一个错误 在循环的第一次迭代 一个成功发射。

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

我希望能够调用 myfunc() 并在 for 循环的每次迭代中向我的 socketio 客户端发出更新

最佳答案

当我拨打 await 时,我也遇到了同样的错误在 non async功能。
例如

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

async erroneous_function():
    await do_something()
解决方法很简单,去掉await前面do_something()因为它不是异步的。

关于python-asyncio - 类型错误 : object NoneType can't be used in 'await' expression,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56872323/

相关文章:

python - 如何使用 aiohttp 确定每秒请求数?

python避免事件​​处理线程中的忙等待

python - 用于组合异步迭代器的映射、过滤器和 itertools

python - 如何在异步 Python 函数中指定返回类型?

python - Flask-SocketIO 和 400 错误请求

python - 关闭使用 asyncio.ensure_future 启动的子任务

google-app-engine - 使用 Google Cloud Platform 运行 Websockets

python - 实时视频处理设计问题

python - 将控制台日志输出重定向到flask-socketio

python - 使用 gunicorn 服务时 flask 超时