SimpleFork Project address: https://github.com/huyanping/simple-fork-phpMulti-process process concurrency framework based on PCNTL extension, the interface is similar to Java's Thread and RunnableWhy write SimpleFork Writing multi-process programs is more complicated than writing multi-threads. Issues such as process recycling, synchronization, mutual exclusion, and communication need to be considered. For beginners, it will be difficult to deal with the above problems. Especially in the area of signal processing and process communication, it is difficult to avoid problems. SimpleFork provides a set of process control interfaces similar to JAVA multi-threading, providing recycling, synchronization, mutual exclusion, communication and other solutions. Developers can focus on business issues and do not need to think too much about process control. Introduction composer require jenner/simple_forkrequirepath/to/SimpleFork/autoload.phpDependency Required
ext-pcntl process control
Optional
ext-sysvmsg message queue
ext-sysvsem synchronization mutex lock
ext -sysvshm shared memory
Features
Provide process pool
Automatically handle zombie process recycling, support non-blocking calls
Provide shared memory, System V message queue, Semaphore lock to facilitate IPC communication (process communication)
Provides two methods of Process and Runnable to implement the process
You can get the process status in real time
When shutting down all processes or stopping a process alone, you can register and override the beforeExit() method, return true to exit, false to continue running (in some scenarios , the process cannot exit immediately)
Supports reload when the child process is running
Notes
The System V message queue will not be destroyed because there may be unprocessed data when the program exits. If it needs to be destroyed, please call the $queue->remove() method to delete the queue
The shared memory will be deleted after all processes exit
The Semaphore object will be destroyed when the object is recycled
After the process pool start(), you need to call wait() can be called non-blockingly for zombie process recycling
Before obtaining the process status (calling the isAlive() method), it is best to call a non-blocking wait(false) for recycling, because the judgment of the process running status is not an atomic operation , so the isAlive() method does not guarantee that it is completely consistent with the actual status. If you don’t know under what circumstances you need to add declare(ticks=1); at the beginning of the program, then it is best to add this statement to the first line by default. .
How to use declare(ticks=1); declare(ticks=1); This declaration is used for process signal processing. If a signal handler is registered, the program will automatically check if there are any unhandled signals before executing a line of code. http://php.net/manual/zh/control-structures.declare.php
TODO
提供更多功能的进程池,模仿java
提供第三方进程通信机制(Redis等)
更多的测试及示例程序
示例程序 更多示例程序见exmples目录simple.phpclassTestRunnableextends\Jenner\SimpleFork\Runnable{/** * 进程执行入口 * @return mixed */publicfunctionrun() {echo"I am a sub process".PHP_EOL; }}$process=new\Jenner\SimpleFork\Process(newTestRunnable());$process->start();shared_memory.phpclassProducerextends\Jenner\SimpleFork\Process{publicfunctionrun(){for($i=0; $i<10; $i++){$this->cache->set($i, $i);echo"set {$i} : {$i}".PHH_EOL; } }}classWorkerextends\Jenner\SimpleFork\Process{publicfunctionrun(){sleep(5);for($i=0; $i<10; $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();
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