1. Go to this page and download the library: Download aternos/taskmaster library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?phprequire_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
aternos / taskmaster example snippets
// Every task is its own class, the class should be autoloadedclassSleepTaskextends \Aternos\Taskmaster\Task\Task{
// The run method is called when the task is executedpublicfunctionrun(): void{
sleep(1);
}
}
// The taskmaster object holds tasks and workers
$taskmaster = new \Aternos\Taskmaster\Taskmaster();
// Set up the workers automatically
$taskmaster->autoDetectWorkers(4);
// Add tasks to the taskmasterfor ($i = 0; $i < 8; $i++) {
$taskmaster->runTask(new SleepTask());
}
// Wait for all tasks to finish and stop the taskmaster
$taskmaster->wait()->stop();
classCallbackTaskextends \Aternos\Taskmaster\Task\Task{
staticprotected int $current = 0;
#[OnParent]publicfunctiongetCurrent(): int{
returnstatic::$current++;
}
#[OnChild]publicfunctionrun(): void{
$current = $this->call($this->getCurrent(...));
echo"I am task number $current\n";
}
}
// Your own task factory extending the TaskFactory classclassSleepTaskFactoryextends \Aternos\Taskmaster\Task\TaskFactory{
protected int $count = 0;
publicfunctioncreateNextTask(?string $group): ?\Aternos\Taskmaster\Task\TaskInterface{
if ($this->count++ < 100) {
returnnew SleepTask();
}
// Stop creating tasks after 100 tasksreturnnull;
}
}
$taskmaster->addTaskFactory(new SleepTaskFactory());
// Create an iterator that iterates over all files in the current directory
$iterator = new \RecursiveIteratorIterator(new \RecursiveDirectoryIterator("."));
// Create the task factory using the iterator and a task class that gets the iterator value as constructor argument// Note, that the SplFileInfo object that you get from a DirectoryIterator is not serializable and therefore cannot be // stored in a task property, but you can use any other values, e.g. the file path
$factory = new \Aternos\Taskmaster\Task\IteratorTaskFactory($iterator, FileTask::class);
$taskmaster->addTaskFactory($factory);
$worker = new \Aternos\Taskmaster\Environment\Sync\SyncWorker();
$options = new \Aternos\Taskmaster\TaskmasterOptions();
$options->setBootstrap(__DIR__ . '/vendor/autoload.php');
$options->setPhpExecutable('/usr/bin/php');
$worker = new \Aternos\Taskmaster\Environment\Process\ProcessWorker();
$worker->setOptions($options);
$proxy = new \Aternos\Taskmaster\Proxy\ProcessProxy();
$worker = new \Aternos\Taskmaster\Environment\Fork\ForkWorker();
$worker->setProxy($proxy);
$options = new \Aternos\Taskmaster\TaskmasterOptions();
$options->setBootstrap(__DIR__ . '/vendor/autoload.php');
$options->setPhpExecutable('/usr/bin/php');
$proxy = new \Aternos\Taskmaster\Proxy\ProcessProxy();
$proxy->setOptions($options);
// Add a single worker
$taskmaster->addWorker(new \Aternos\Taskmaster\Environment\Sync\SyncWorker());
// Add a worker multiple times
$taskmaster->addWorkers(new \Aternos\Taskmaster\Environment\Process\ProcessWorker(), 4);
// Add multiple workers with the same proxy
$worker = new \Aternos\Taskmaster\Environment\Fork\ForkWorker();
$worker->setProxy(new \Aternos\Taskmaster\Proxy\ProcessProxy());
$taskmaster->addWorkers($worker, 8);
// Define/replace all workers at once
$taskmaster->setWorkers([
new \Aternos\Taskmaster\Environment\Fork\ForkWorker(),
new \Aternos\Taskmaster\Environment\Fork\ForkWorker(),
new \Aternos\Taskmaster\Environment\Fork\ForkWorker(),
]);
// Load count and workers from environment variables
$taskmaster->autoDetectWorkers(4);
// Load nothing from environment variables
$taskmaster->autoDetectWorkers(4, false);
// Load worker types from environment variables, but keep the worker count
$taskmaster->autoDetectWorkers(4, true, false);
// init tasks are always provided by a task factory
$taskmaster->setDefaultInitTaskFactory(new InitTaskFactory());
// but taskmaster can create task factories automatically by cloning or instancing a task
$taskmaster->setDefaultInitTask(new InitTask());
$taskmaster->setDefaultInitTask(InitTask::class);
// you can also define a task factory for a specific worker
$worker->setInitTaskFactory(new InitTaskFactory());
do {
$finishedTasks = $taskmaster->update();
// do something else
} while ($taskmaster->isRunning());
$taskmaster->stop();
// create a group A with 4 fork workers
$workerA = new \Aternos\Taskmaster\Environment\Fork\ForkWorker();
$workerA->setGroup('A');
$taskmaster->addWorkers($workerA, 4);
// create a group B with 2 process workers
$workerB = new \Aternos\Taskmaster\Environment\Process\ProcessWorker();
$workerB->setGroup('B');
$taskmaster->addWorkers($workerB, 2);
// create tasks that only run on group Afor ($i = 0; $i < 10; $i++) {
$taskA = new SleepTask();
$taskA->setGroup('A');
$taskmaster->runTask($taskA);
}
// create tasks that only run on group Bfor ($i = 0; $i < 5; $i++) {
$taskB = new FileTask();
$taskB->setGroup('B');
$taskmaster->runTask($taskB);
}