pe / component-process
基于 ext-pcntl 和 ext-posix 的进程管理
v1.0.0
2019-01-11 05:54 UTC
Requires
- php: >=7.0
- psr/log: ^1.1
Requires (Dev)
- phpunit/phpunit: ~6.0
Suggests
- ext-pcntl: *
- ext-posix: *
- ext-proctitle: *
This package is auto-updated.
Last update: 2024-09-11 19:05:14 UTC
README
以下版本的 PHP 受支持。
- PHP 7.0+
安装
要安装,请使用 composer
php composer.phar require pe/component-process
使用方法
简单管理器使用
<?php namespace PE\Component\Process; // Instantiate manager $manager = new Manager(); // Create process $process = new Process(function (Process $process) { //TODO do some stuff... }); // Execute process $manager->fork($process); $manager->fork($process); $manager->fork($process); $manager->fork($process);// <-- this will be ignored because we set max executed processes // Wait until processes completed $manager->wait();
将任何长时间执行的代码恶魔化
<?php namespace PE\Component\Process; use Psr\Log\NullLogger; // Define path to pid file, must be writable by php user $pidPath = sys_get_temp_dir() . '/daemon.pid'; // Instantiate a daemon $daemon = new Daemon(function () { //TODO do some stuff... }, $pidPath); // Instantiate logger $logger = new NullLogger(); // Start execution in background $daemon->start($logger); // You can check if daemon is still running by call: $daemon->isRunning(); // You can break execution by call: $daemon->stop($logger);