# Simple threading question

**URL:** <https://www.ruby-forum.com/t/simple-threading-question/195624>\
**Category:** NGINX\
**Created:** [September 27, 2010, 8:05am UTC](https://www.ruby-forum.com/t/simple-threading-question/195624 "2010-09-27T08:05:51Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![ankur](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/91b2a8/32.png) [@ankur](https://www.ruby-forum.com/u/ankur)\
**Post date:** [September 27, 2010, 8:05am UTC](https://www.ruby-forum.com/t/simple-threading-question/195624/1 "2010-09-27T08:05:51Z")

</div>

Hi,

I have a bunch of nginx and php-fpm worker threads. I am running a  
very simple test with a script doing: \<? sleep(20); ?\>. I notice that  
if I make two requests at the same time (from the same IP address), they  
get  
serialized on the  
php-fpm side and have a twenty second differential. Why would this  
happen and the two requests not get processed in parallel when there  
are an abundant number of threads? Here are the two requests in the  
slow log file and they have a twenty second difference:

Sep 26 19:02:38.177091 pid 13597 (pool default)  
script\_filename = /var/www/test1/test.php  
[0x00000000095b5280] sleep() /var/www/test1/test.php:2

Sep 26 19:02:58.321560 pid 13609 (pool default)  
script\_filename = /var/www/test1/test.php  
[0x000000000942a890] sleep() /var/www/test1/test.php:2

I have 100+ nginx threads  
$ ps -afe | grep nginx | wc -l  
120

and 150 php-fpm threads  
$ ps -afe | grep php-fpm | wc -l  
150

Thanks,  
Ankur

---

<div class="post-metadata">

**Author:** ![ankur](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/91b2a8/32.png) [@ankur](https://www.ruby-forum.com/u/ankur)\
**Post date:** [September 27, 2010, 8:17am UTC](https://www.ruby-forum.com/t/simple-threading-question/195624/2 "2010-09-27T08:17:33Z")

</div>

nginx doesnt need 100+ threads, it uses an event worker model. Set  
your worker\_processes to something more reasonable. If you are trying  
to handle 150 requests then you really only need a single worker.

---

<div class="post-metadata">

**Author:** ![ankur](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/91b2a8/32.png) [@ankur](https://www.ruby-forum.com/u/ankur)\
**Post date:** [September 27, 2010, 8:50am UTC](https://www.ruby-forum.com/t/simple-threading-question/195624/3 "2010-09-27T08:50:55Z")

</div>

I can change the number of threads but my question is something  
completely  
orthogonal to what you replied. Why did the two server requests to  
php-fpm  
get serialized and not happen in parallel even though there are enough  
worker threads?

---

<div class="post-metadata">

**Author:** ![ankur](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/91b2a8/32.png) [@ankur](https://www.ruby-forum.com/u/ankur)\
**Post date:** [September 27, 2010, 9:19am UTC](https://www.ruby-forum.com/t/simple-threading-question/195624/4 "2010-09-27T09:19:36Z")

</div>

On Sun, Sep 26, 2010 at 11:05:00PM -0700, Ankur G. wrote:

> 120
> 
> and 150 php-fpm threads  
> $ ps -afe | grep php-fpm | wc -l  
> 150

nginx does not serialize requests to FastCGI. A single nginx worker can  
handle tens thousand concurrent FastCGI connections.

–  
Igor S.

> **[Igor Sysoev](http://sysoev.ru/en/)**

---

<div class="post-metadata">

**Author:** ![ankur](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/91b2a8/32.png) [@ankur](https://www.ruby-forum.com/u/ankur)\
**Post date:** [September 27, 2010, 9:28am UTC](https://www.ruby-forum.com/t/simple-threading-question/195624/5 "2010-09-27T09:28:58Z")

</div>

On 27/09/2010 07:50, Ankur G. wrote:

> I can change the number of threads but my question is something  
> completely orthogonal to what you replied. Why did the two server  
> requests to php-fpm get serialized and not happen in parallel even  
> though there are enough worker threads?

It may be that nginx is proxying to the same php process, which has  
multiple threads, and your sleep command in that thread is causing the  
process to sleep. I’ve found that the best set-up is 1/2 nginx workers  
(depending on how many CPUs/cores you’re running and how much SSL  
traffic you have), multiple PHP processes, each running a small number  
(\<5) threads. Specifically, or main production server runs 2/20/4. If  
you need to issue sleep() commands and it _is_ locking the process  
rather than the specific thread, maybe reduce that to 2/30-50/1?

–

_Phillip B Oldham_  
ActivityHQ  
[removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) [mailto:removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

* * *

_Policies_

This e-mail and its attachments are intended for the above named  
recipient(s) only and may be confidential. If they have come to you in  
error, please reply to this e-mail and highlight the error. No action  
should be taken regarding content, nor must you copy or show them to  
anyone.

This e-mail has been created in the knowledge that Internet e-mail is  
not a 100% secure communications medium, and we have taken steps to  
ensure that this e-mail and attachments are free from any virus. We must  
advise that in keeping with good computing practice the recipient should  
ensure they are completely virus free, and that you understand and  
observe the lack of security when e-mailing us.

---

<div class="post-metadata">

**Author:** ![ankur](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/91b2a8/32.png) [@ankur](https://www.ruby-forum.com/u/ankur)\
**Post date:** [September 27, 2010, 8:57am UTC](https://www.ruby-forum.com/t/simple-threading-question/195624/6 "2010-09-27T08:57:57Z")

</div>

Well the simple answer is they dont, check you php-fpm settings (if  
using dynamic process manager then threads = in use not spare etc)

---

<div class="post-metadata">

**Author:** ![ankur](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/91b2a8/32.png) [@ankur](https://www.ruby-forum.com/u/ankur)\
**Post date:** [September 27, 2010, 2:01pm UTC](https://www.ruby-forum.com/t/simple-threading-question/195624/7 "2010-09-27T14:01:11Z")

</div>

On 27/09/2010 07:05, Ankur G. wrote:

> I have a bunch of nginx and php-fpm worker threads. I am running a  
> very simple test with a script doing: \<? sleep(20); ?\>. I notice that  
> if I make two requests at the same time (from the same IP address), they  
> get serialized on the  
> php-fpm side and have a twenty second differential. Why would this  
> happen and the two requests not get processed in parallel when there  
> are an abundant number of threads?

This is a feature of PHP.

It carefully sequences the accesses so that only one thread at a time  
can access the session variables.

You can call session\_write\_close() after you have updated all your  
session variables, to tell php you are done with the session and it will  
release any waiting thread.

If nginx is serving static content, these serves will not be effected.

Regards

Ian

---

<div class="post-metadata">

**Author:** ![ankur](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/91b2a8/32.png) [@ankur](https://www.ruby-forum.com/u/ankur)\
**Post date:** [September 27, 2010, 5:50pm UTC](https://www.ruby-forum.com/t/simple-threading-question/195624/8 "2010-09-27T17:50:12Z")

</div>

Ian, nice pickup never conidered session locking.

Another soln, dont start a session on the blocking page if it is not  
needed.

Sent from my iPod
