https://github.com/edwardstock/forker
PHP POSIX process manager and async ProcessPool
https://github.com/edwardstock/forker
multithreading phpasync
Last synced: about 1 month ago
JSON representation
PHP POSIX process manager and async ProcessPool
- Host: GitHub
- URL: https://github.com/edwardstock/forker
- Owner: edwardstock
- License: gpl-3.0
- Created: 2016-12-01T13:39:41.000Z (over 9 years ago)
- Default Branch: master
- Last Pushed: 2016-12-19T12:53:58.000Z (over 9 years ago)
- Last Synced: 2025-12-14T17:08:00.362Z (7 months ago)
- Topics: multithreading, phpasync
- Language: PHP
- Homepage:
- Size: 53.7 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# PHP Forker
PHP POSIX process manager and async ProcessPool
[](https://travis-ci.org/edwardstock/forker)
# News
* 1.0.2
* process pooling fix and added method to run process with limited count per "now":
```php
pool(2 /*pool size*/, true /*join processes*/);
for($i = 0; $i < 10; $i++) {
// when 2 jobs will added to pm queue, they will run, and cleaned after complete
// calling $pm->run() manually not needed
$pm->add($job);
}
```
* 1.0.1
* Added support for custom arguments in background funciton
## Features
* Easy to create multi-processed daemons
* POSIX Signals dispatching
* Serializing objects/arrays that contains closures (thx to [SuperClosure](https://github.com/jeremeamia/super_closure))
* Uses shared memory
* *.pid file managing
## Usage examples
#### Basic usage
```php
future(function($updatedCount, CallbackTask $task) use(&$updated) {
$updated = $updatedCount;
})->error(function(\Throwable $exception, CallbackTask $task){
// handle exception occurred while DB::bigQuery()
Logger::log($exception);
});
$processManager = new ProcessManager('/path/to/file.pid');
$processManager
->add($bigTableUpdate)
->run(true) // true - join to main process, if you don't have an expensive and complex logic in future method
->wait(); // wait while process will complete doing job
// if don't call wait() method, process will be detached from terminal or main process and continue to working in background
echo $updated; // count of updated very_big_table
```
That was just a very simple example, now more useful
#### Batch usage
```php
future(function ($sitesContent, BatchTask $task) use (&$results) {
$results = $sitesContent;
});
$pm = new ProcessManager();
$pm->add($downloads);
$pm->run(true)->wait();
var_dump($results);
// result
// array(2) {
// 0 => string(28) 'html_content_from_google.com'
// 1 => string(30) 'html_content_from_facebook.com'
// }
// Order of results in this case is random, cause, for example,
// facebook.com can be downloaded faster than google.com
```
More examples will soon... ;)