What exactly is a pre-fork web server model?

Pre-forking basically means a master creates forks which handle each request. A fork is a completely separate *nix process.

Pre-forking can be used when you have libraries that are NOT thread safe. It also means issues within a request causing problems will only affect the process which they are processed by and not the entire server.

The initialisation running multiple times all depends on what you are deploying. Usually however connection pools and stuff of that nature would exist for each process.

In a threading model the master would create lighter weight threads to dispatch requests too. But if a thread causes massive issues it could have repercussions for the master process.

With tools such an Nginx, Apache 2.4’s Event MPM, or gevent (which can be used with Gunicorn) these are asynchronous meaning a process can handle hundreds of requests whilst not blocking.

 

 

Prefork – For simplicities sake lets say that each process requires 1% cpu and 10mb of RAM.

/———\
| Prefork | ——– > Request — 1% CPU – 10 MB RAM
\________/

/———\
| Prefork | ——– > Request — 1% CPU – 10 MB RAM
\________/

/———\
| Prefork | ——– > Request — 1% CPU – 10 MB RAM
\________/

/———\
| Prefork | ——– > Request — 1% CPU – 10 MB RAM
\________/

So in the instance above we have 4 processes which use up 4% of your CPU and 40MB of RAM.

Prefork – We’ll use similar numbers here but lets assume that the prefork has a little more overhead than the process model per process.

/————-\
| Worker MPM | ———-> Request \
| Worker MPM | ———-> Request | All these request use up 2% CPU and 25 MB of ram.
| Worker MPM | ———-> Request |
| Worker MPM | ———-> Request /
\____________/

Although that is a very simple explanation you can get the general idea