toolkit / sys-utils
PHP的一些系统工具库
v2.0.5
2022-12-02 06:16 UTC
Requires
- php: >8.0.0
- toolkit/stdlib: ~2.0
Suggests
- inhere/console: a lightweight php console application library.
- toolkit/cli-utils: useful cli tool library of the php
README
一些有用的PHP系统工具
- 执行系统命令
- 简单的进程使用
- PHP环境信息
- 错误和异常信息
安装
composer require toolkit/sys-utils
使用
进程任务
示例:
use Toolkit\Sys\Proc\ProcTasks; // ProcTasks::new() // will auto get cpu num as proc num ProcTasks::new(['procNum' => 2]) ->setTaskHandler(function (array $task, array $ctx) { $pid = $ctx['pid']; println("worker#{$ctx['id']} [PID:$pid] - handle task, task data", $task); sleep(random_int(1, 3)); }) ->onBeforeCreate(fn($pid, $num) => println("master [PID:$pid] - Will create task process, number:", $num)) ->onWorkersCreated(fn($pid, $info) => println("master [PID:$pid] - All task process started,", 'Workers info', $info)) ->onWorkerStart(fn($pid, $id) => println("worker#$id started, pid is", $pid)) ->onWorkerExit(fn($pid, $id) => println("worker#$id exited, pid is", $pid)) ->onCompleted(fn($pid) => println("master [PID:$pid] - all workers exited, tasks run completed")) ->setTasks([ ['task1'], // one task ['task2'], ['task3'], ['task4'], ]) ->addTask(['task5']) ->run();
运行:
php example/proc-tasks.php
输出:
master [PID:49731] - Will create task process, number: 2
master [PID:49731] - All task process started, Workers info {"49732":{"id":0,"pid":49732,"startAt":1639245860},"49733":{"id":1,"pid":49733,"startAt":1639245860}}
worker#0 started, pid is 49732
worker#0 [PID:49732] - handle task, task data ["task1"]
worker#1 started, pid is 49733
worker#1 [PID:49733] - handle task, task data ["task4"]
worker#1 [PID:49733] - handle task, task data ["task5"]
worker#0 [PID:49732] - handle task, task data ["task2"]
worker#1 exited, pid is 49733
worker#0 [PID:49732] - handle task, task data ["task3"]
worker#0 exited, pid is 49732
master [PID:49731] - all workers exited, tasks run completed