【问题标题】:TypeError: 'coroutine' object is not iterableTypeError:“协程”对象不可迭代
【发布时间】:2022-06-29 17:01:00
【问题描述】:

我使用 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

这是我的 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 

在 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

提前致谢

【问题讨论】:

  • 请尽可能详细地说明触发此错误的原因以及在您的代码中触发的位置。请不要仅仅链接您的整个源代码树并期望人们搜索有错误的代码。
  • 对错误感到抱歉。我提供了有关在 nginx 中完成的项目和主管配置的更多详细信息。

标签: python python-asyncio coroutine aiohttp python-socketio


【解决方案1】:

通过--worker-class aiohttp.GunicornWebWorker:

gunicorn ... --worker-class aiohttp.GunicornWebWorker

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

【讨论】:

    猜你喜欢
    • 2013-09-01
    • 2017-08-27
    • 2018-10-10
    • 2021-12-13
    • 2019-02-20
    • 2020-03-27
    • 2018-12-12
    • 2018-07-16
    • 2011-09-12
    相关资源
    最近更新 更多