In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/02 Report--
This article introduces the relevant knowledge of "the principle and use of PHP multi-process". In the operation of actual cases, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!
Prepare for
We all know that PHP is executed by a single process, and PHP mainly relies on multiple processes of server or PHP-FPM and their reuse, but it is also significant for PHP to implement multiple processes, especially when dealing with large amounts of data or running background DEMON daemons in background Cli mode, the advantages of multi-processes are needless to say.
The multithreading of PHP has also been mentioned, but the problem of resource sharing and allocation of multithreading within the process is difficult to solve. PHP also has an extension pthreads that multiple threads want to turn off, but it is said to be unstable and requires a thread-safe environment that is not used much.
In the past, a great god in the PHP group once instructed that the background PHP must be multi-process if you want to advance, and it just so happens that the daemon process in the company also applied the multi-process of PHP, combined with Google's various materials and manuals, finally understood the multi-process, and wrote a small demo (realized on the linux system). Use this article to summarize, if there are mistakes or omissions, thank you for raising.
To implement the multi-process of PHP, we need two extensions pcntl and posix. The installation method will not be discussed here.
Create a child process
Creating a PHP child process is the beginning of multiple processes, and we need the pcntl_fork () function
Detailed explanation of fork function
Pcntl_fork ()-generates branches (child processes) at the current location of the current process. After this function creates a new child process, the child process will inherit the current context of the parent process and continue to execute downward from the pcntl_fork () function like the parent process, except that the return value of pcntl_fork () is different, so we can distinguish the parent process from the child process by judging the return value, and assign the parent process and the child process to do different logical processing.
When the pcntl_fork () function executes successfully, it returns the process id (pid) of the child process in the parent process, because the pid of the system's initial process init process is 1, and the pid of the later generated process will be larger than this process, so we can confirm that the current process is the parent process by judging that the return value of pcntl_fork () is greater than 1.
In a child process, the return value of this function will be a fixed value of 0. We can also determine the child process by judging that the return value of pcntl_fork () is 0.
When the pcntl_fork () function fails, it returns-1 in the parent process, and of course there are no child processes.
Fork process instance
The following is a simple example of a fork subprocess:
$ppid = posix_getpid (); $pid = pcntl_fork (); if ($pid = =-1) {throw new Exception ('cause child process failed!');} elseif ($pid > 0) {cli_set_process_title ("I am the parent process, my process id is {$ppid}."); sleep (30) / / hold for 30 seconds to ensure that} else {$cpid = posix_getpid (); cli_set_process_title ("I am a child of {$ppid} and my process id is {$cpid}."); sleep (30);}
At this point, let's introduce two functions:
Posix_getpid (): gets the pid of the current process
Cli_set_process_title ('resounding name'): give the current process a loud name.
By running this example, we can see the current two PHP processes.
This is the end of the introduction of "the principle and use of PHP multi-process". Thank you for reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.