https://github.com/paveldanilin/process-executor
The ProcessExecutor executes closure in sub-process.
https://github.com/paveldanilin/process-executor
cron parallel php scheduler
Last synced: 7 months ago
JSON representation
The ProcessExecutor executes closure in sub-process.
- Host: GitHub
- URL: https://github.com/paveldanilin/process-executor
- Owner: paveldanilin
- Created: 2021-09-05T14:09:02.000Z (over 4 years ago)
- Default Branch: main
- Last Pushed: 2021-11-22T01:57:34.000Z (over 4 years ago)
- Last Synced: 2025-07-20T08:51:06.349Z (8 months ago)
- Topics: cron, parallel, php, scheduler
- Language: PHP
- Homepage:
- Size: 56.6 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.MD
Awesome Lists containing this project
README
# ProcessExecutor
ProcessExecutor allows you to execute the closure in the sub-process.
Due to the limitations of the serialization process, it is not possible to pass any PHP resource to the closure.
But we can overcome this drawback by opening a resource inside closure (e.g. open a db connection or file).
Scheduled execution
```php
$executor = ProcessExecutors::newScheduledPoolExecutor(4);
$periodSec = 1;
$executor->schedule($periodSec, function () {
// Do a time-consuming task
// This code block will be executed in sub-process
\sleep(5);
$result = \time();
return $result;
})->onFulfilled(function ($time) {
echo "Time: $time\n";
});
$executor->start();
```
Parallel execution
```php
$executor = ProcessExecutors::newFixedPoolExecutor(4);
$executor->submit(function () {
// Do a time-consuming task
sleep(2);
return 1;
})->then(function ($data) {
print $data . "\n";
});
// With timeout
$timeoutSec = 2;
$executor->submit(function () {
// Do a time-consuming task
sleep(10);
return 2;
}, $timeoutSec)->then(function ($data) {
print $data . "\n";
});
$executor->submit(function () {
// Do a time-consuming task
sleep(5);
return 3;
})->then(function ($data) {
print $data . "\n";
});
$executor->waitAll();
```
Cron
```php
$executor = ProcessExecutors::newScheduledPoolExecutor(4);
// Hourly
$executor->cron('0 * * * *', function () {
// Do a time-consuming task
\file_put_contents('./cron.out', (new \DateTime())->format('H:i:s') . "\n", FILE_APPEND);
});
$executor->start();
```
See more [examples](./examples).