M
M
MichaelMih2021-11-24 17:59:45
Flask
MichaelMih, 2021-11-24 17:59:45

Why won't celery start?

Hello! There is a code:

spoiler
from logging import debug
from flask import Flask
from flask_socketio import SocketIO
from celery import Celery
import threading


app = Flask("server", static_url_path="", static_folder='/')
app.config['SECRET_KEY'] = 'gjr39dkjn344_!67#'
app.config['broker_url'] = 'redis://127.0.0.1:6379/0'
app.config['result_backend'] = 'redis://127.0.0.1:6379/0'


def make_celery(app):
    celery = Celery(
        "server",
        backend=app.config['broker_url'],
        broker=app.config['result_backend']
    )
    celery.conf.update(app.config)

    class ContextTask(celery.Task):
        def __call__(self, *args, **kwargs):
            with app.app_context():
                return self.run(*args, **kwargs)

    celery.Task = ContextTask
    return celery


celery = make_celery(app)

@celery.task()
async def my_background_task():
    f = open("text.txt", "w")
    f.write("fsdsdf")
    return "sss"

@app.route("/")
async def main():
    task = my_background_task.delay()<code></code>
    print(task)
    return "sss"


After the launch, the celery starts working, but there is no flask, if I add app.run, the selery stops working, but the flask starts. How can it be solved?

I start Celery like this: celery --app server.celery worker --loglevel info

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
Dimonchik, 2021-11-27
@dimonchik2013

good Celery code
should be run according to the instructions
https://docs.celeryproject.org/en/stable/userguide...
and at that time forget about the existence of anything at all, in particular the flask

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question