Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python multiprocessing in flask

This question has probably been asked, and more than likely answered, but I don't know where to find it here.

Problem: I have a router for python's flask, that takes a while to process data for each call. I need to make each of the calls to the routes be a thread in itself so it doesn't have to wait for the requests to be loaded.

like image 547
Corbbin Goldsmith Avatar asked Feb 25 '16 01:02

Corbbin Goldsmith


People also ask

Does flask use multiprocessing?

Modern web servers like Flask, Django, and Tornado are all able to handle multiple requests simultaneously. The concept of multitasking is actually very vague due to its various interpretations. You can perform multitasking using multiprocessing, multithreading, or asyncio.

Can flask handle multiple requests?

How many concurrent requests can Flask handle? 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 we do multiprocessing in Python?

Python's Global Interpreter Lock (GIL) only allows one thread to be run at a time under the interpreter, which means you can't enjoy the performance benefit of multithreading if the Python interpreter is required. This is what gives multiprocessing an upper hand over threading in Python.

What is Python multiprocessing?

multiprocessing is a package that supports spawning processes using an API similar to the threading module. The multiprocessing package offers both local and remote concurrency, effectively side-stepping the Global Interpreter Lock by using subprocesses instead of threads.


1 Answers

Flask comes with a built-in development web server, but you shouldn't be using it in production.

To get cool features like separate processes for each request and static file serving, you need to run an actual web service and a WSGI service in front of your Flask application.

The Flask docs provide several examples on how to set that up. Popular Web Server/WSGI combinations are Apache/mod_wsgi and Nginx/Gunicorn, but there are many other options.

like image 94
Brendan Abel Avatar answered Sep 28 '22 19:09

Brendan Abel