如何在PHP中处理并发请求(使用-线程,线程池或子进程) [英] how are concurrent requests handled in PHP (using - threads, thread pool or child processes)

查看:175
本文介绍了如何在PHP中处理并发请求(使用-线程,线程池或子进程)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我了解PHP支持处理多个并发连接,并且根据服务器的不同,可以按 answer

I understand that PHP supports handling multiple concurrent connections and depending on server it can be configured as mentioned in this answer

服务器如何管理多个连接,它是为每个请求派生一个子进程,还是使用线程处理还是使用线程池处理?

How does server manages multiple connections does it forks a child process for each request or does it handle using threads or does it handles using a thread pool?

链接的答案表示过程是分叉的,然后在注释中的作者说线程或进程,如果使用子进程,线程或线程池来处理请求,则会造成混乱?

The linked answer says a process is forked and then the author in comment says threads or process, which makes it confusing, if requests are served using child-processes, threads or thread pool?

推荐答案

每个Web服务器都有它自己的多种多样的同时处理请求。
通常,Apache2应该为每个新请求派生一个子进程。但是您可以按照链接的StackOverflow答案中所述配置此行为。

As I know, every webserver has it's own kind of handling multpile simultanous request. Usually Apache2 schould fork a child process for each new request. But you can somehow configure this behaviour as mentioned in your linked StackOverflow answer.

例如,Nginx在一个线程中获取每个请求(像Node.js一样异步处理新连接)或有时使用缓存(根据配置; Nginx也可以用作负载平衡器或HTTP代理)。为您的应用程序选择合适的网络服务器。

Nginx for example gets every request in one thread (processes new connections asyncronously like Node.js does) or sometimes uses caching (as configured; Nginx could also be used as a load balancer or HTTP proxy). It's a thing of choosing the right webserver for your application.

Apache2可能是一个非常好的网络服务器,但是要在生产环境中使用它,您需要更多的负载平衡。但是,当拥有多个短时连接或什至根本不更改的文档(或使用缓存)时​​,它也具有强大的功能。

Apache2 could be a very good webserver but you need more loadbalancing when you want to use it in production. But it also has good power when having multiply short lasting connections or even documents which don't change at all (or using caching).

Nginx非常有用许多持久的连接以及某种程度的处理时间。

Nginx is very good if you expect many long lasting connections with somehow long processing time. You don't need that much loadbalancing then.

我希望,我能够为您提供帮助;)

I hope, I was able to help you out with this ;)

来源:

https://httpd.apache.org/docs/2.4/mod/worker.html

https://anturis.com/blog/nginx-vs-apache/

我建议您也查看以下内容:在PHP中是线程安全还是非线程安全?

I recommend you to also look at: What is thread safe or non-thread safe in PHP?

这篇关于如何在PHP中处理并发请求(使用-线程,线程池或子进程)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆