rogerwaters/react-thread-pool

Multiprocessing library for PHP based on pcntl_fork() and reactphp

dev-master 2016-06-09 12:38 UTC

This package is not auto-updated.

Last update: 2024-12-21 19:33:32 UTC


README

This is a multiprocessing library for PHP based on pcntl_fork().

Status: alpha
Tests: none

Features

  • Running Parallel Processes out of a single Process
  • Give the entire environment to the Thread without thinking on serialisation
  • Control the thread (Start, Stop, Kill) + Custom messages
  • Running tons of threads on an single ThreadPool (be careful with your processor)
  • Setting up threads to observe external resources

Requirements

Basic Usage

Create an EventLoop:

$loop = ForkableFactory::create();

*The loop is the same as reactphp/event-loop so you can also use this for your server
Creating a default thread to perform heavy work outside your parent process:

use RogerWaters\ReactThreads\EventLoop\ForkableLoopInterface;
use RogerWaters\ReactThreads\ThreadBase;

class ExampleThread extends ThreadBase
{
    protected function InitializeExternal(ForkableLoopInterface $loop)
    {
        //Do your external logic here
        //you can also use $loop functions
        //Use $this->kill(); to complete execution from child
        $this->kill();
    }
}

All together: ```php //create thread $thread = new ExampleThread($loop); //start thread and do external logic $thread->start();

//wait for the thread to complete $thread->on('stopped',function() use ($loop) { //thread is done //stop the parent process $loop->stop(); });

//you can do any other operations here without affecting the thread

//run the loop to wait for completion $loop->run();


For communicable threads see example_3.php and example_4.php

## More examples
See /examples folder

## TODO:
- Documentation
- Tests
- More examples
- https://github.com/reactphp/event-loop/issues/41
- Stability / Error handling

If you have any issues or feature request, feel free to create a ticket