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 } ?> Copy after login 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); } } ?> Copy after login 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. |

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

Alipay PHP...

JWT is an open standard based on JSON, used to securely transmit information between parties, mainly for identity authentication and information exchange. 1. JWT consists of three parts: Header, Payload and Signature. 2. The working principle of JWT includes three steps: generating JWT, verifying JWT and parsing Payload. 3. When using JWT for authentication in PHP, JWT can be generated and verified, and user role and permission information can be included in advanced usage. 4. Common errors include signature verification failure, token expiration, and payload oversized. Debugging skills include using debugging tools and logging. 5. Performance optimization and best practices include using appropriate signature algorithms, setting validity periods reasonably,

Session hijacking can be achieved through the following steps: 1. Obtain the session ID, 2. Use the session ID, 3. Keep the session active. The methods to prevent session hijacking in PHP include: 1. Use the session_regenerate_id() function to regenerate the session ID, 2. Store session data through the database, 3. Ensure that all session data is transmitted through HTTPS.

The application of SOLID principle in PHP development includes: 1. Single responsibility principle (SRP): Each class is responsible for only one function. 2. Open and close principle (OCP): Changes are achieved through extension rather than modification. 3. Lisch's Substitution Principle (LSP): Subclasses can replace base classes without affecting program accuracy. 4. Interface isolation principle (ISP): Use fine-grained interfaces to avoid dependencies and unused methods. 5. Dependency inversion principle (DIP): High and low-level modules rely on abstraction and are implemented through dependency injection.

How to debug CLI mode in PHPStorm? When developing with PHPStorm, sometimes we need to debug PHP in command line interface (CLI) mode...

Article discusses essential security features in frameworks to protect against vulnerabilities, including input validation, authentication, and regular updates.

How to automatically set the permissions of unixsocket after the system restarts. Every time the system restarts, we need to execute the following command to modify the permissions of unixsocket: sudo...

Static binding (static::) implements late static binding (LSB) in PHP, allowing calling classes to be referenced in static contexts rather than defining classes. 1) The parsing process is performed at runtime, 2) Look up the call class in the inheritance relationship, 3) It may bring performance overhead.
