Python Flask: How add requests to a queue and rearrange the tasks in queue before executing

Viewed 6684

A bit of the context:

want to write an algorithm that accepts tickets from the client. Sorts them on some constraints, handles them, and replies back to the client with the results.

I did some research and though REST API of python is a good idea. But as I explored it I found out that, It usually is built to handle one request at once.

Is there a way to add tasks(REST API requests) to the queue, sort them and execute them with workers and reply back to clients once processing is done?

1 Answers

I can suggest three ways to do that.

  1. Try to use database to store the request content, constraint and status as 'pending'. Later, when you want to trigger the processing of the requests just retrieve them in sorted order by your constraint and update the status to 'processed'.
  2. You can use Redis task queue with flask. See the article. https://realpython.com/flask-by-example-implementing-a-redis-task-queue/
  3. You can also use Celery module with Flask. See the documentation. https://flask.palletsprojects.com/en/1.1.x/patterns/celery/
Related