Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is PHP under Apache reentrant?

Tags:

php

lamp

Just a theoretical question really.

say my website consists of a form that uses the PHP mail functions to send e-mails. I have 500 users clicking submit at the same time. Now 500 e-mails in 500 different sessions have to be sent out from PHP.

will it be done concurrently? how many threads are involved? will each send block the others and do it one by one?

like image 845
JasonGenX Avatar asked Mar 08 '11 18:03

JasonGenX


1 Answers

There are two things you need to think about.

The first is how you have your web server configured. If you're using Apache, there are a few processing modules that can be picked from. The most popular processing module is prefork, in which there is a single parent process and multiple child processes. Each child handles one request at a time. This avoids threading entirely, because not all Apache modules are thread safe. You might also find the worker module somewhere in production. It uses a combination of forking behavior and threading to serve multiple requests per child. It can only be used when every single Apache module and all of it's dependencies are thread safe.

The second thing to think about is PHP itself. While the core PHP language and some extensions are thread safe, many extensions aren't thread safe. For this reason, when you're using Apache and mod_php, the prefork processing module is your best choice. (PHP itself has no internal concept of threads.)

tl;dr: Apache + PHP = one request per Apache child. You'll usually only have 20-30 Apache children, meaning 20-30 possible concurrent requests. This depends on configuration.

like image 187
Charles Avatar answered Oct 11 '22 22:10

Charles