TypeError: 'coroutine' object is not iterable
Asked Answered
C

3

8

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

Correctitude answered 27/5, 2022 at 5:19 Comment(0)
I
10

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.

Inanna answered 12/2, 2023 at 13:1 Comment(2)
You need to add "await" to the caller function if the called fuction is "async"Clothespin
That was it! I missed one in the chain. Sarcastically, that error couldn't be more clear!Chaps
T
0

Pass --worker-class aiohttp.GunicornWebWorker:

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

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

Thermoscope answered 29/6, 2022 at 9:1 Comment(0)
C
0

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

Chari answered 17/9 at 11:30 Comment(1)
Your answer could be improved with additional supporting information. Please edit to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers in the help center.Bigener

© 2022 - 2024 — McMap. All rights reserved.