Home > Article > Backend Development > Understand the wonderful use of php multi-threading
This article introduces the wonderful use of PHP multi-threading in the Linux environment. Friends in need can refer to it.
The PHP multi-threading knowledge shared in this section requires the use of PHP's pcntl_fork function. This function depends on the implementation of the operating system fork. The above content is only applicable to Linux/Unix systems. Let’s take a look at the usage of pcntl_fork function: <?php $pid = pcntl_fork(); if ($pid == -1) { die('could not fork'); } else if ($pid) { // we are the parent pcntl_wait($status); //Protect against Zombie children } else { // we are the child } ?> Create a child process via pcntl_fork. If the return value is -1, then the child process creation failed. The successfully created process ID will be returned to the parent process, and 0 will be returned to the child process. It’s customary to write like this: <?php $pid = pcntl_fork(); if($pid == -1){ //创建失败咱就退出呗,没啥好说的 die('could not fork'); } else{ if($pid){ //父进程代码,由于是系统程序,请退出值给出返回值 exit(0); } else{ //在新的进程中执行的程序,正常退出的话,也需要返回值 exit(0); } } ?> If the parent process wants to know that the child process exits normally, you can add the previous pcntl_wait. Let’s talk about its role in actual development. 1, Background program Command line programs are easy to write, as are service programs. I think this service program is the most difficult to write. Linux wants a command line program to run in the background, just add an & after the command. But this always feels boring. With pcntl_fork, I suddenly found that the world is so beautiful. When the main process successfully creates a child process and obtains the child process After the id, I did not forget to say before dying: "I have successfully run, my id is: xxxx (the id of the child process)", and when it is finished, the system returns 0 (normal exit). What I mentioned earlier is that the program resides in memory. Pay attention to the release of memory and printing information to the log file instead of to the screen (the program will exit as soon as the information is printed). Another situation is: the program is called by other scripts , other scripts only care about whether the program is running normally. If the program takes a long time to run, it is best not to let the script wait. In this case, pcntl_fork comes in handy again:) 2, Delayed processing. Sometimes when a program exits, it needs to clean up what it has produced, such as deleting itself (of course, running files can be deleted under Linux, just for example). At this time, you can start another process and then end it yourself. , handing things over to another process. When we write a service program, we must write a log file to record the running status of the program (otherwise, who knows if the program is sleeping there: 0). The program exits normally We can write a log saying that the program exited, but when the program receives the great kill -9 under Linux, how to record its own exit behavior? Um... This is related to the process signal of PHP and it seems to have nothing to do with this. How big a deal. Case 2: A complete program generally supports parameters such as start, stop, and restart. Start is easy to say, and stop is easy to say. Since start and stop are easy to say, for restart, just stop first and then start. Uh. ..It seems to have nothing to do with pcntl_fork. When receiving the restart signal, you can't just kill and then start again. A gentler approach is to exit the current process and let another process pull it up again. 3, Immortal process This is the so-called dual process. |