T
T
tem12qaz2020-12-25 22:45:01
Python
tem12qaz, 2020-12-25 22:45:01

How to run aiogram with gunicorn?

I just started to deal with gunicorn, so please do not judge for illiteracy.

I needed to launch the aiogram bot via webhook. It worked fine without gunicorn (through ngnix), but I read that gunicorn is needed and decided to try with it.

The problem is that gunicorn (as I understand it) takes as arguments a program and a function from that program that returns a web application (in my case aiohttp). Aiogram, when executing the start_webhook() function, does not return the application, but immediately launches it.

If you change the start_webhook() function to return the aiohttp app instead of starting it, and run it through gunicorn: gunicorn main:get_web_app --bind localhost:8001 --worker-class aiohttp.GunicornWebWorker
an error occurs: RuntimeError: This event loop is already running in

python Perhaps it is not needed for a telegram bot?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
D
Dr. Bacon, 2020-12-25
@bacon

It worked fine without gunicorn (through ngnix), but I read that gunicorn is needed and decided to try with it.
And why is gunicorn in this bundle needed, didn’t you read it? If before that it worked fine, why change something, or were there reasons for this?
Threat about, there is generally aiohttp, it can normally work even without ngnix.

I
Ilya Zverev, 2021-05-08
@Zverik

That's right, when calling set_webhook, aiogram does not expect the loop to already be running. Therefore, I stupidly tore out the relevant pieces of code from aiogram.utils.executor:

async def async_app():
    app = make_app()
    executor = Executor(dp, skip_updates=True)
    executor.on_startup(on_startup)
    executor.on_shutdown(on_shutdown)
    executor._prepare_webhook(config.WEBHOOK_PATH, app=app)
    await executor._startup_webhook()
    return app

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question