skip to Main Content

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

2

Answers


  1. 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

    Login or Signup to reply.
  2. 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.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search