Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I manage a queue of requests in my Flask service?

I am using flask to get POST requests from the users and process the data without returning any response.

To process the user requests the solution that I currently have is:

def process_data(request_data):
    # do_the_processing will process the data 
    do_the_processing(request_data)
    # Terminate the spawned process
    sys.exit(0)

@app.route('/', methods =['POST'])
def index():
    request_data = request.get_json()
    p = Process(target=process_data, args=(request_data,))
    p.start()
    return '200'

But this will spawn a new process for every user request. What if 1000 users make a request to the service at the same time. OS won't be able to handle these many processes. I am ready to make the users wait until their turn comes in the queue. do_the_processing method uses another web API to process the data and then write files in the memory.

I need to maintain a queue in which I can put the request as it arrives and then process the request_data on first come first serve basis.

To increase the processing we can use 4 processes to get the data from the queue and process it. Once the data has been processed we'll fetch another data from the queue and process it but the catch is we should use only 4 processes to do the processing.

The current solution will spawn 1000 processes if 1000 users make a request to the server.

How can I implement this type of functionality, any ideas?

like image 409
Savage Panda Avatar asked Mar 24 '18 05:03

Savage Panda


People also ask

How do you handle multiple requests in Flask?

Flask applications are deployed on a web server, either the built-in server for development and testing or a suitable server (gunicorn, nginx etc.) for production. By default, the server can handle multiple client requests without the programmer having to do anything special in the application.

How do you handle requests in Flask?

Any flask application, while handling a request, creates a Request object. This object created is dependent on the environment as received by the WSGI server. A worker is able to handle only one request at a time. As the request is made, the flask application automatically pushes a request context during the course.

How does Flask handle multiple concurrent requests?

Flask will process one request per thread at the same time. If you have 2 processes with 4 threads each, that's 8 concurrent requests. Flask doesn't spawn or manage threads or processes.

Can Flask take multiple requests?

Yes, deploy your application on a different WSGI server, see the Flask deployment options documentation. The server component that comes with Flask is really only meant for when you are developing your application; even though it can be configured to handle concurrent requests with app.


Video Answer


1 Answers

I've never found a good way of using the built in queue for sharing a work queue between processes. By that I mean gunicorn/uwsgi processes not manager/workers.

My suggestion would be to use something like redis or rabbit mq for distributing the workload. There are several frameworks for this such as celery or just using pika. This would build your application scaleable in a way. Beging able to distribute API, work queue and workers on separate instances in what scale you require.

like image 78
Tobias Avatar answered Oct 21 '22 16:10

Tobias