类型错误:“协程”对象不可迭代

Sha*_*lim 5 python coroutine python-asyncio aiohttp python-socketio

我使用 Aiohttp 服务器和 python-socket-io 构建了一个聊天应用程序。当我尝试在 nginx 中托管此应用程序时,我在主管错误日志中发现了此错误(错误日志路径= /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
Run Code Online (Sandbox Code Playgroud)

这是我的 aiohttp 服务器设置。

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 
Run Code Online (Sandbox Code Playgroud)

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
Run Code Online (Sandbox Code Playgroud)

提前致谢

aar*_*ron 0

经过--worker-class aiohttp.GunicornWebWorker

gunicorn ... --worker-class aiohttp.GunicornWebWorker
Run Code Online (Sandbox Code Playgroud)

参考: https: //docs.aiohttp.org/en/stable/web_advanced.html#passing-a-coroutine-into-run-app-and-gunicorn