Skip to main content
If your main app process has been successfully deployed on Sevalla, adding a background task worker only requires a few additional steps. For this example, we use Celery and Redis; however, the same approach can be applied to other task queues, such as Huey and RQ. Add both celery and redis to your dependencies.
In the extensions.py file, add a celery_init_app function. The following is the same starter code used by the Flask documentation.
Create a simple task in a tasks.py file to verify Celery is working properly.
In __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.
At the same level as 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.
If you want to run Celery in your local environment, add the following to your .env.

Deploy on Sevalla

Within Sevalla, create a Redis database and connect it to your app. Make sure you change the REDIS_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.
Inside the shell, run the following:
The addition of 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.