SimpleForkphp multi-process concurrency framework

Source: Internet
Author: User
: This article mainly introduces the SimpleForkphp multi-process concurrency framework. if you are interested in the PHP Tutorial, refer to it. SimpleFork

Project address: https://github.com/huyanping/simple-fork-php

The multi-process concurrency framework based on PCNTL extension. The interface is similar to the Java Thread and Runnable

Why do you want to write SimpleFork?

Multi-process programming is more complex than multi-thread programming. process collection, synchronization, mutex, communication, and other issues must be considered. It is difficult for beginners to solve the above problems. Especially for signal processing and process communication, it is difficult to solve the problem. SimpleFork provides a set of JAVA-like multi-thread process control interfaces, and provides solutions such as recycling, synchronization, mutex, and communication. developers can focus on business issues without having to consider process control too much.

Introduction

Composer require jenner/simple_fork

Requirepath/to/SimpleFork/autoload. php

Dependency

Required

  • Ext-pcntl process control
  • Optional

  • Ext-sysvmsg Message Queue
  • Ext-sysvsem synchronous mutex lock
  • Ext-sysvshm shared memory
  • Features

  • Process pool
  • Automatic processing of zombie process recycling and support for non-blocking calls
  • Provides shared memory, System V message queue, and Semaphore locks to facilitate IPC communication (process communication)
  • Process implementation in two ways: Process and Runnable
  • Process status can be obtained in real time
  • When shutdown all processes or stop one process separately, you can register the override beforeExit () method, exit if true is returned, and continue running if false is returned (in some scenarios, the process cannot exit immediately)
  • Support for reload during sub-process running
  • Notes

  • The System V message queue does not destroy data that has not been processed when the program exits. To destroy the queue, call $ queue-> remove () to delete the queue.
  • The shared memory will be deleted after all processes exit.
  • Semaphore object will be destroyed when the object is recycled
  • After the process pool start (), you need to call wait () to recycle zombie processes.
  • Before obtaining the process status (calling the isAlive () method), it is best to call a non-blocking wait (false) for a recovery. because the process running status is determined not an atomic operation, isAlive () the method is not necessarily consistent with the actual status
  • If you do not know under what circumstances you need to add declare (ticks = 1) at the beginning of the program, 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 processor is registered, the program will automatically check whether there are unprocessed signals after a line of code is executed. Http://php.net/manual/zh/control-structures.declare.php
  • TODO

  • Provides more function process pools to simulate java
  • Provides third-party process communication mechanisms (such as Redis)
  • More test and example programs
  • Sample program

    For more examples, see the exmples directory.

    Simple. php

    ClassTestRunnableextends \ Jenner \ SimpleFork \ Runnable {/*** process execution entry * @ return mixed */publicfunctionrun () {echo "I am a sub process ". PHP_EOL; }}$ process = new \ Jenner \ SimpleFork \ Process (newTestRunnable (); $ process-> start ();

    Shared_memory.php

    ClassProducerextends \ 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 ();

    Original article, reprinted please note: Reprinted from always not enough

    Link: SimpleFork php multi-process concurrency framework

    Reprinted please note: always not enough? SimpleFork php multi-process concurrency framework

    The preceding section describes the SimpleFork php multi-process concurrency framework, including some content, and hopes to help those who are interested in PHP tutorials.

    Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.