TypeError:' coroutine'对象是不可能的

发布于 2025-02-01 16:33:53 字数 2209 浏览 5 评论 0原文

我使用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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

眼中杀气 2025-02-08 16:33:53

我遇到了同样的问题,错误是由于不使用等待 async函数。

在我的情况下,我有一个async函数,该函数返回了字典,然后另一个字典通过该字典进行了更新,这就是我正在做的:

async def make_dict():...

def update_dict(dict1):
   dict1.update(make_dict())

但是,为了解决错误,应该更改它如下:

async def make_dict():...

def update_dict(dict1):
   dict1.update(await make_dict())

在您的情况下,我认为您应该考虑在调用index函数之前放置等待

I had the same issue, the error was from not using await for async 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:

async def make_dict():...

def update_dict(dict1):
   dict1.update(make_dict())

But in order to resolve the error, it should be changed like the following:

async def make_dict():...

def update_dict(dict1):
   dict1.update(await make_dict())

In your situation, I think you should consider putting an await before calling the index function.

浮光之海 2025-02-08 16:33:53

在这里,在异步定义中应使用等待来调用异步定义。

Here, Async defination should be called using await in async defination.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文