gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-04 12:11:00 25 4
gpt4 key购买 nike

我试图从我的 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com