Combining Flask with Celery: Maximizing Efficiency with Parallel Processes

My Flask Celery app instantiates the celery instance. I'm aware that I can add a normal Flask route to the same .py file, but would need to run the code twice:

  1. To run the worker:

    % celery worker -A app.celery ...

  2. To run the code as a normal Flask app:

    % python app.py ...

My query is: Since the normal Flask app runs separately from the Celery app, how can I interact with the running celery instance from a Flask route to perform actions like:

  celery.control.purge()
  celery.control.inspect() etc ???

Below is my code snippet:

import os
import random
import time
from flask import Flask, request, render_template, session, flash, redirect, \
    url_for, jsonify
from celery import Celery

app = Flask(__name__)

# Celery configuration
app.config['CELERY_BROKER_URL'] = 'redis://localhost:6379/0'
app.config['CELERY_RESULT_BACKEND'] = 'redis://localhost:6379/0'

# Initialize Celery
celery = Celery(app.name, broker=app.config['CELERY_BROKER_URL'])
celery.conf.update(app.config)

@celery.task
def send_async_email(msg):
    """Background task to send an email with Flask-Mail."""
    with app.app_context():
        mail.send(msg)

@app.route('/purge', methods=['GET', 'POST'])
def purge_tasks():
    ## want to do stuffs with the running celery instance, e.g:
    ## doing:
    ##    celery.control.purge()
    ##    celery.control.inspect()
    ##
    ## BUT HOW??

if __name__ == '__main__':
    app.run(debug=True)

I've looked for answers online but haven't found any that address this specific question.

Any help or pointers would be greatly appreciated. Thank you!

Answer №1

Here is a helpful resource that can provide the solution you are looking for: . Make sure to carefully go through the sections on Application and Calling the Task. To implement the task in your Flask app, follow the instructions outlined in the documentation by saving your Flask app as a "task" and then integrating it into a Celery instance referred to as "app."

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Dealing with JSON object as a string in API using Laravel with PHP

I have a Python API that generates a JSON object with data on staff behavior. The response looks like this: { "Text":{ "0":"Very unfriendly staff at reception: not responding to needs and giving wrong information.", "1":"The staff are polit ...

Obtain product pricing information from a JSON file

Trying to fetch product details from an adidas API using the code snippet below: import requests url = "https://www.adidas.com/api/plp/content-engine?" params = { 'sitePath': 'us', 'query': 'women-athl ...

difficulties encountered while making an API request

I am trying to make an API call to get the forecast air pollution data from this website The website provides the following API call format: http://api.openweathermap.org/data/2.5/air_pollution/forecast?lat={latitude}&lon={longitude}&appid={API_ke ...

What is the best way to swap out characters according to the user's input?

new = ['|<', '@', '1', '3', '7', '0', '8', '\/'] old = ['K', 'A', 'L', 'E', 'T', 'O', 'B', 'V ...

Leveraging selenium for writing messages directly to Chrome's console

Is there a way to write directly to the Chrome console using Selenium and Python? I'm not looking for a solution that involves sending keys to open the console. If direct writing is not possible, are there any libraries or APIs that allow this functio ...

SeleniumLibrary does not recognize the parameter executable_path

Transitioning from pytest + Selenium to robotframework + SeleniumLibrary + Selenium has presented some challenges for me. Even with SeleniumLibrary's simplified keyword structure, I have encountered difficulties performing basic operations that were s ...

Discovering the method to locate and interact with a concealed file upload field using Selenium WebDriver in

Here is the HTML code I am working with: <a id="buttonToUpload" class="btn-pink medium" href="#"> <span class="icon-arrow-right">upload photo</span> </a> <form id="form1" enctype="multipart/form-data"> <input id="u ...

Unable to extract text from a span element while web scraping with Selenium

Trying to scrape job listings from LinkedIn using Selenium but unable to retrieve text from a specific tag. The code snippet in question is as follows: company_details = wd.find_element(By.XPATH,"//div[@class='mt5mb2']/li[1]").find_elements(By.TA ...

Navigating through a multi-level dataframe using Python

I have encountered JSON values within my dataframe and am now attempting to iterate through them. Despite several attempts, I have been unsuccessful in converting the dataframe values into a nested dictionary format that would allow for easier iteration. ...

Using Python 3 to switch window background on button press

After spending some time utilizing this website, I must say it has been incredibly beneficial for my Linux and Python needs - so thank you :) Recently, I embarked on the journey of adding a background to an application window. I created a button that trigg ...

Ways to enhance the size of aircraft in Plotly

Upon analyzing the code snippet import pandas as pd import plotly.graph_objects as go import numpy as np df = pd.read_csv('https://raw.githubusercontent.com/tiago-peres/immersion/master/Platforms_dataset.csv') fig = px.scatter_3d(df, x='Fu ...

When saving data to a CSV file in Python, the information is separated by commas, ensuring each character is properly recorded

Trying to read data from a CSV file and then write it to another one. However, the written data is separated by commas. Below is the code snippet: with open(filenameInput, 'r') as csvfile: dfi = pd.read_csv (filenameInput) dfi - dfi.ilo ...

Error arises in Python when strings are compared

I'm having an issue with this code where it's not properly comparing strings and I've exhausted all possible avenues for finding the problem: If anyone could assist me, the files are being read correctly but the comparison is not happening ...

Python Implementation of Bag-of-Words Model with Negative Vocabulary

I am working with a unique document It's not your typical text It's full of scientific terminologies The content of this document looks like this RepID,Txt 1,K9G3P9 4H477 -Q207KL41 98464 ... Q207KL41 2,D84T8X4 -D9W4S2 -D9W4S2 8E8E65 ... D9W4S ...

Retrieve query results and organize them using the group_by method in the model structure, then have them displayed in

Currently, I am facing an issue with my model form where I am attempting to populate a dropdown select with options from the database. This is how my model form is structured: class CreateTripsForm(forms.Form): start_locations = Mileage.objects.value ...

Can the creation of an Allure report be done within Python's unittest framework?

Is there a way to generate an allure report in python-unittest, similar to how it's done in Pytest? ...

Utilizing the Database Model within HTML Templates in DJango

I need help accessing a value from another table in my HTML code. Instead of using {{ i.user }}, I want to retrieve a value that matches with {{ i.user }}. How can I achieve this? {% for i in obj reversed %} <div class="container"> <blockquote> ...

Ways to obtain values from the DataArray within XArray

I am seeking the elevation data for specific points from tif files. I want to extract only the value associated with latitude and longitude coordinates. How can I retrieve just the 451.13458 value in the array(451.13458, dtype=float32) of the DataArray? ...

Encountering a TypeError stating that list indices should be integers, not strings, while attempting to access data from a dictionary

The code snippet below showcases my attempt to access the value of "role" from a dictionary and list stored in the variable "data." However, I encountered an error while trying to do so: import json data = {"users":[{"user_id":"11w","device_id":"AQ","rol ...

The process of "encrypting" a message using a specific set of guidelines often yields an unfinished outcome

I need to develop a function that encrypts a user's message and returns the encrypted string based on specific rules: Alphabetic Uppercase Character [A-Z]: Convert to lowercase and add a ^ character at the end Alphabetic Lowercase Character [a-z]: R ...