marlon
marlon

Reputation: 7703

Multi processing or threads in http service?

I am using Flask to host a http service:

import requests
r = requests.get('http://0.0.0.0:5000/segment?text=this is a test')

When a client calls this service, it has to process text one by one. I have 2 questions regarding this:

1) In order to make use of Python's multiprocessings or multithreads, can the client send multi requests concurrently to this http service to speed it up?

2) If it is yes to question 1), does this 'segment' have to support multiprocessing? I can make it use multiprocessing, but the parameters have to a list of text, not a single text.

Generally, in this http service context, how to make the http requests to be processed faster, hopefully both from client and server side (i.e. 'segment' function)?

Upvotes: 0

Views: 193

Answers (1)

justahuman
justahuman

Reputation: 637

In production, you won't expose the flask http server to the world. You will expose a Gunicorn interface (uWSGI) which will dispatch several worker threads. You won't need to import multiprocessing and write the logic yourself.

What this means is that if Client A and B send a request at the same time, Gunicorn will dispatch 2 threads to handle both the requests and those threads may return a response when they terminate (for the sake of simplicity, we can say that they return at the same time).

So the answer is yes, the client can send multiple requests synchronously at once. Your server will be able to handle multiple requests if you set up Gunicorn (which is recommended in the Flask docs).

Upvotes: 1

Related Questions