TypeError:' coroutine'对象是不可能的
我使用AIOHTTP服务器并使用Python-Socket-io构建ChatApplication。当我尝试在NGINX中托管此应用程序时,我发现主管中的错误log(错误log path =/var/log/gunicorn/gunicorn/gunicorn.err.err.log)中的错误log中发现了此错误
[2022-05-27 04:16:31 +0000] [32957] [ERROR] Error handling request
/chatserver Traceback (most recent call last):
File "/home/ubuntu/env/lib/python3.8/site-packages/gunicorn/workers/sync.py", line 136, in handle
self.handle_request(listener, req, client, addr)
File "/home/ubuntu/env/lib/python3.8/site-packages/gunicorn/workers/sync.py", line 184, in handle_request
for item in respiter:
TypeError: 'coroutine' object is not iterable
。
import socketio
from aiohttp import web
import aiohttp_cors
# create aiohttp application
app = web.Application()
# creates a new Async Socket IO Server
sio = socketio.AsyncServer(
cors_allowed_origins='*',
cors_credentials=True
)
# Binds our Socket.IO server to our Web App
sio.attach(app)
cors = aiohttp_cors.setup(app)
# user esatblish connection with server
@sio.event
def connect(sid, environ):
@sio.event
def set_online(sid, data):
"""
set user sid in the dictionary
"""
print(sid, data)
async def index(request):
return web.Response(text="Welcome home!")
async def my_web_app():
# ==================== Endpoints =========================
app.router.add_get('/index', index)
# ==================== Endpoints =========================
"""
supervisor execute
command( /home/ubuntu/env/bin/gunicorn --workers 3 --bind unix:/home/ubuntu/host-hustle-website/chatapp/app.sock chat:my_web_app )
my_web_app func will excecute and app is the created web application(aiohttp instace) is return
"""
return app
主管设置在Nginx中
[program:aio-server]
command=/home/ubuntu/env/bin/gunicorn --workers 3 --bind unix:/home/ubuntu/host-hustle-website/chatapp/app.sock chat:my_web_app
directory=/home/ubuntu/host-hustle-website/chatapp
autostart=true
autorestart=true
stderr_logfile=/var/log/gunicorn/gunicorn.err.log
stdout_logfile=/var/log/gunicorn/gunicorn.out.log
[group:guni]
programs=aio-server
提前感谢
I build a chatapplication using Aiohttp server and using python-socket-io. When i tried to host this application in nginx i found this error in supervisor error from error log of supervisor(error log path= /var/log/gunicorn/gunicorn.err.log )
[2022-05-27 04:16:31 +0000] [32957] [ERROR] Error handling request
/chatserver Traceback (most recent call last):
File "/home/ubuntu/env/lib/python3.8/site-packages/gunicorn/workers/sync.py", line 136, in handle
self.handle_request(listener, req, client, addr)
File "/home/ubuntu/env/lib/python3.8/site-packages/gunicorn/workers/sync.py", line 184, in handle_request
for item in respiter:
TypeError: 'coroutine' object is not iterable
This is my aiohttp Server Setup.
import socketio
from aiohttp import web
import aiohttp_cors
# create aiohttp application
app = web.Application()
# creates a new Async Socket IO Server
sio = socketio.AsyncServer(
cors_allowed_origins='*',
cors_credentials=True
)
# Binds our Socket.IO server to our Web App
sio.attach(app)
cors = aiohttp_cors.setup(app)
# user esatblish connection with server
@sio.event
def connect(sid, environ):
@sio.event
def set_online(sid, data):
"""
set user sid in the dictionary
"""
print(sid, data)
async def index(request):
return web.Response(text="Welcome home!")
async def my_web_app():
# ==================== Endpoints =========================
app.router.add_get('/index', index)
# ==================== Endpoints =========================
"""
supervisor execute
command( /home/ubuntu/env/bin/gunicorn --workers 3 --bind unix:/home/ubuntu/host-hustle-website/chatapp/app.sock chat:my_web_app )
my_web_app func will excecute and app is the created web application(aiohttp instace) is return
"""
return app
Supervisor setup in nginx
[program:aio-server]
command=/home/ubuntu/env/bin/gunicorn --workers 3 --bind unix:/home/ubuntu/host-hustle-website/chatapp/app.sock chat:my_web_app
directory=/home/ubuntu/host-hustle-website/chatapp
autostart=true
autorestart=true
stderr_logfile=/var/log/gunicorn/gunicorn.err.log
stdout_logfile=/var/log/gunicorn/gunicorn.out.log
[group:guni]
programs=aio-server
Thanks in advance
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我遇到了同样的问题,错误是由于不使用
等待
async
函数。在我的情况下,我有一个
async
函数,该函数返回了字典,然后另一个字典通过该字典进行了更新,这就是我正在做的:但是,为了解决错误,应该更改它如下:
在您的情况下,我认为您应该考虑在调用
index
函数之前放置等待
。I had the same issue, the error was from not using
await
forasync
functions.In my situation, I had an
async
function which returned a dictionary , and then another dictionary got updated by that dictionary, This is what I was doing:But in order to resolve the error, it should be changed like the following:
In your situation, I think you should consider putting an
await
before calling theindex
function.在这里,在异步定义中应使用
等待
来调用异步定义。Here, Async defination should be called using
await
in async defination.