Home  >  Article  >  Backend Development  >  socket - using php multi-threading, multi-process? ?

socket - using php multi-threading, multi-process? ?

WBOY
WBOYOriginal
2016-09-21 14:13:141390browse

I. What is the usage of php multi-process? ? (used by most php programs | almost never used)

II. What is the usage of php multi-threading? ? (used by most php programs | almost never used)

III. How do php multi-process and php multi-thread compare with java? ? (simply terrible | not even close)

Almost everyone on the Internet writes that PHP is not suitable for multi-threading (is it also not suitable for multi-process??). Does this mean that there is no need to understand PHP-related features such as multi-threading and multi-process? ?

And recently, due to personal preferences, I want to make a web-based chat tool similar to QQ. Now I basically understand socket communication, but I have learned about open source communication frameworks like Workerman. His feature introduction includes the following:

socket - using php multi-threading, multi-process? ?

socket - using php multi-threading, multi-process? ?

socket - using php multi-threading, multi-process? ?

I. Can PHP multi-process take advantage of multi-core CPUs, improve performance, and support high concurrency? ?

II. A single php process can support thousands or even tens of thousands of concurrent connections, and multiple processes can support hundreds of thousands or even millions of concurrent connections. How to understand this sentence? ? Since it uses sockets for communication, the number of ports is actually fixed and will not exceed 65536. One port represents a pair of connections. The thousands mentioned here -" tens of thousands may be better, multi-process Are the hundreds of thousands or millions of bets so powerful? ?

Reply content:

I. How is the usage of php multi-process? ? (used by most php programs | almost never used)

II. What is the usage of php multi-threading? ? (used by most php programs | almost never used)

III. How do php multi-process and php multi-thread compare with java? ? (simply terrible | not even close)

It is almost written on the Internet that PHP is not suitable for multi-threading (is it also not suitable for multi-process??). Does this mean that there is no need to understand PHP-related features such as multi-threading and multi-process? ?

And recently, due to personal preferences, I want to make a web chat tool similar to QQ. Now I basically understand socket communication, but I have learned about open source communication frameworks like Workerman. His feature introduction includes the following:

socket - using php multi-threading, multi-process? ?

socket - using php multi-threading, multi-process? ?

socket - using php multi-threading, multi-process? ?

I. Can PHP multi-process take advantage of multi-core CPUs, improve performance, and support high concurrency? ?

II. A single php process can support thousands or even tens of thousands of concurrent connections, and multiple processes can support hundreds of thousands or even millions of concurrent connections. How to understand this sentence? ? Since it uses sockets for communication, the number of ports is actually fixed and will not exceed 65536. One port represents a pair of connections. The thousands mentioned here -" tens of thousands may be better, multi-process Are the hundreds of thousands or millions of bets so powerful? ?

Correct a concept: the total number of ports is an unsigned short, which is 65535. This does not mean that a server can only support so many connections! One port corresponds to one process, and the number of connections of a process is consistent with the number of socket descriptors opened by this process. The number of descriptors is on the order of 32-bit int. Of course, the system has a limit on the maximum descriptors of a process, which can be configured through kernel parameters, but hundreds of thousands are definitely no problem.

The working principle of a typical multi-process http server is roughly like this: a process is responsible for monitoring the connection on port 80. When the connection arrives, the corresponding descriptor is created by the kernel, and then the process finds a relatively idle child process To process this descriptor, that is, read and write data on this descriptor. Note that this is a child process. It is precisely because of the child process that descriptors can be passed between parent and child processes. If the number of accesses increases and the number of child processes is insufficient, the parent process can create new child processes to handle descriptors as appropriate; the parent process should implement a simple load balancing strategy to a certain extent.

The working principle of the http server based on multi-threading is actually similar. Changing the above process to thread can also be explained.

The above explanation has nothing to do with PHP. PHP just encapsulates these underlying concepts and system calls. Most people use php which is based on the php script interpreter. And workerman should be an http service implemented based on php-encapsulated system calls.

  • The php process is managed by php-fpm (implementation of fastcgi): the master process will distribute requests to child processes

    • master process: service communicates with web server

      • Communication implementation method 1: http socket (host + port), will occupy one port, so that means php-fpm only occupies one port here

      • Communication implementation method two: unix domain socket (inter-process communication), which is based on .sock file and does not rely on network protocols, so in theory it does not occupy ports

    • pool process: child process, including parser, parsing script

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn