celery and redis to your dependencies.
extensions.py file, add a celery_init_app function. The following is the same starter code used by the Flask documentation.
tasks.py file to verify Celery is working properly.
__init__.py, pass the Flask app instance to this new function so it can be properly initialized. You’ll also need to import the debug_task function to ensure Celery discovers it at startup. In a real-world application, this explicit import is usually unnecessary, as tasks are typically imported indirectly through your route or module imports.
run.py, create a file named make_celery.py to serve as the entry point for your Celery worker. This file is responsible for initializing Celery with your application’s configuration. The load_dotenv function is used to load values from the .env file into the environment when Celery starts, which is only necessary for local development. Environment variables are provided automatically when running on Sevalla.
.env.
Deploy on Sevalla
Within Sevalla, create a Redis database and connect it to your app. Make sure you change theREDIS_URL environment variable to FLASK_CELERY__broker_url when adding the environment variables from your Redis instance. If you also want to use Redis to store your Celery results, add the value FLASK_CELERY__result_backend.
To start the Celery worker, create a new background worker with the following start command:
celery -A make_celery worker -c 1 -l INFO
Celery is started by referencing the name of the entry point file. The concurrency is set to one here to avoid overuse of your resources. You can adjust the value to match the needs of your app.
Once you deploy your app, you’ll see Celery startup information in your logs.
To test the debug task defined above, you can go to the web terminal and manually trigger the debug task.
Inside the web terminal, start the virtual environment and run the task from the Flask shell.
delay to the debug_task call sends the task to the Celery broker instead of running it directly. Within the runtime logs, you can view the output message.