Simplefork
Project Address: https://github.com/huyanping/simple-fork-php
Multi-process process concurrency framework based on PCNTL extension, interface similar to Java thread and runnable
Why do you write simplefork
Multi-process programming is more complicated than thread writing, and it is necessary to consider process recycling, synchronization, mutex, communication and so on. It is difficult for beginners to deal with the above problems. In particular, signal processing and process communication this piece, it is difficult to do without problems. Simplefork provides a set of Java multi-threading Process control interfaces that provide recycling, synchronization, mutex, and communication solutions, and developers can focus on business issues without much consideration of process control.
Introduced
Composer require jenner/simple_fork
requirepath/to/simplefork/autoload . PHP
Depend on
Have to
EXT-PCNTL Process Control Options available
Ext-sysvmsg Message Queuing
Ext-sysvsem Synchronous Mutex Lock
EXT-SYSVSHM Shared Memory Characteristics
Providing a process pool
Automatic processing of zombie process recycling, support for non-blocking calls
Provides shared memory, System V Message Queuing, Semaphore lock for easy IPC communication (process communication)
Provides process and runnable two ways to implement processes
Process status can be obtained in real time
Shutdown all processes or individual stop a process, you can register the overwrite Beforeexit () method, return True to exit, false to continue running (in some scenarios, the process cannot exit immediately)
Support for child process runtime reload Precautions
System V Message Queuing is not destroyed because there may be data that has not yet been processed when the program exits. If you need to destroy, call the $queue->remove () method to delete the queue
Shared memory is removed after all processes have exited
The semaphore object is destroyed when the object is recycled
After the process pool start (), you need to call Wait () to recycle the zombie process, which can be called without blocking
Before getting the process state (call the IsAlive () method), it is better to call a non-blocking wait (false) to recycle, because the process running state is not determined by atomic operations, so the IsAlive () method is not guaranteed to be exactly the same as the actual state
If you do not know under what circumstances you need to join declare (Ticks=1) at the very beginning of the program, then it is best to add this statement by default on the first line. How to use Declare (Ticks=1);
Declare (Ticks=1); This statement is used for process signal processing. If a signal processor is registered, the program automatically checks for pending signals without executing a line of code. http://php.net/manual/zh/control-structures.declare.php Todo
A process pool that provides more functionality, mimicking Java
Provides third-party process communication mechanisms (REDIS, etc.)
More Tests and sample programs Sample Program
See the Exmples directory for more sample programs
simple.php
classtestrunnableextends\jenner\simplefork\Runnable{ /** * Process Execution Portal * @return mixed */ Public functionrun() { echo"I am a sub process". Php_eol; } } $process=new\jenner\simplefork\process(new testrunnable()); $process-Start ();
shared_memory.php
classProducerextends\jenner\simplefork\Process{ Public functionrun() { For ($i=0; $i < ten; $i ++) { $this,cache, set ($i, $i); echo"set {$i}: {$i}". Phh_eol; } } } classWorkerextends\jenner\simplefork\Process{ Public functionrun() { Sleep(5); For ($i=0; $i < ten; $i ++) { echo"get {$i}: ". $this - Cache Get ($i) . Php_eol; } } } $memory=new\jenner\simplefork\ipc\sharedmemory(); $producer=newproducer(); $producer-Setcache ($memory); $worker=newworker(); $worker-Setcache ($memory); $pool=new\jenner\simplefork\pool(); $pool, submit ($producer); $pool, submit ($worker); $pool-Start (); $pool, Wait ();
Original articles, reproduced please specify: Reprint from always enough
This article link address: simplefork PHP Multi-process concurrency framework
Reprint Please specify: Always not enough? Simplefork PHP Multi-process concurrency framework
The above describes the Simplefork PHP multi-process concurrency framework, including the aspects of the content, I hope that the PHP tutorial interested in a friend helpful.