rogerwaters / react-thread-pool
Multiprocessing library for PHP based on pcntl_fork() and reactphp
dev-master
2016-06-09 12:38 UTC
Requires
- php: >=5.4.0
- evenement/evenement: dev-master
- react/event-loop: v0.4.*
Suggests
- ext-event: ~1.0
- ext-libevent: >=0.1.0
This package is not auto-updated.
Last update: 2024-11-09 18:59:29 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
- Linux/Unix platform
- PHP >= 5.4 (also PHP 7)
- custom functions (pcntl_fork, pcntl_waitpid, posix_getpid)
- reactphp/event-loop
- igorw/evenement
- [optional] PHP Extension libevent (pect/libevent-0.1.0)
- [optional] PHP Extension Event http://php.net/manual/en/book.event.php
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