derywat/php-processes 问题修复 & 功能扩展

解决BUG、新增功能、兼容多环境部署,快速响应你的开发需求

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

derywat/php-processes

Composer 安装命令:

composer require derywat/php-processes

包简介

PHP process management library

README 文档

README

Library allows to fork existing php process and provide interprocess messaging.

Using ForkManager

Forking new process

use derywat\processes\ForkManager;

$pid = ForkManager::run('uniqueProcessNameOrId',function($socket){
	//new process code...
});

Sending exit signal to child process

Internal implementation: SIGTERM signal will be send to child process. Signal will be handled automatically by ForkManager.

use derywat\processes\ForkManager;

ForkManager::stop('uniqueProcessNameOrId');

Reacting to exit signal in child process

Child process can periodically check if exit was requested.

use derywat\processes\ForkManager;

$pid = ForkManager::run('uniqueProcessNameOrId',function($socket){
	//new process code...

	if(ForkManager::childShouldExit()){
		//custom pre-exit code
		exit();
	}

});

Interprocess messaging

Sending message

Any serializable (by using php internal serialize) data can be passed in message.
Messages can be sent both ways - parent -> child and child->parent.

use derywat\processes\ForkManager;

ForkManager::sendMessage($socket,$data);

Receiving messages

Queued messages can be read periodically by using processMessages method.
Messages array contains objects of type InterprocessMessageInterface. Messages array is ordered by the time of sending message - oldest messages first.

Process finished message is sent on forked child process end.

	$messages = $ForkManager::processMessages();
	if(!empty($messages)){
		foreach($messages as $message){
			//check if process finished
			if($message->isProcessFinished()){
				//this message was sent on process finish
				//handle finished processes here
			} else {
				//message sent by process running at the time of sending message
				$processNameOrId = $message->getName();
				$processPid = $message->getPid();
				$messageData = $message->getData();
				//handle message and its data here
			}
		}
	}

Interprocess commands

Important

added in version 0.1.1

Interprocess commands handling is higher level abstraction for implementing command dispatching and handling in application.

Defining custom application commands

Define custom command as class implementing CommandMessageInterface.
__tostring method must be implemented, but it may return any string.

Example class of log command.

use derywat\processes\commandMessages\CommandMessageInterface;

class CommandMessageLog implements CommandMessageInterface {

	protected $level;
	protected $message;

	public function __construct($level, $message){
		$this->level = $level;
		$this->message = $message;
	}

	public function getLevel() {
		return $this->level;
	}

	public function getMessage() {
		return $this->message;
	}

	//mandatory method
	public function __tostring(){
		$message = $this->getMessage();
		return "command message - log: {$message}";
	}

}

Using CommandMessagesDispatcher

CommandMessagesDispatcher provide single point of commands or commands groups definitions in app.
Implement own dispatcher by extending CommandMessagesDispatcher class.

use derywat\processes\commandMessages\CommandMessagesDispatcher;

class MyCommandMessagesDispatcher extends CommandMessagesDispatcher {

	public static function log($socket, $level, $logMessage){
		static::send($socket,new CommandMessageLog($level,$logMessage));
	}

}

Dispatch command with dispatcher in process code. Provide socket for sending message.

MyCommandMessageDispatcher::log($socket,'info','log message');

Using CommandMessagesHandler in application

CommandMessagesHandler handles commands in application.
Register command class by using class name and providing handling closure.

use derywat\processes\commandMessages\CommandMessagesHandler;

$logger = new MyLoggingClass();

$commandMessagesHandler = (new CommandMessagesHandler())
	->registerCommand(
		CommandMessageLog::class,
		function(CommandMessageLog $command) use ($logger):void {
			$level = $command->getLevel();
			$logMessage = $command->getMessage();
			//call logging code here...
			$logger->log($level,$logMessage);
		}
	);

Access original interprocess message in handler

Important

added in version 0.1.2

Optional second parameter of type InterprocessMessageInterface may be added to command handling closure for accessing interprocess message data.

use derywat\processes\commandMessages\CommandMessagesHandler;

$logger = new MyLoggingClass();

$commandMessagesHandler = (new CommandMessagesHandler())
	->registerCommand(
		CommandMessageLog::class,
		function(CommandMessageLog $command, InterprocessMessageInterface $message) use ($logger):void {
			$level = $command->getLevel();
			$logMessage = $command->getMessage();

			//use message to pass sending process name as log source
			$logSource = $message->getName();

			//call logging code here...
			$logger->log($level,$logMessage,$logSource);
		}
	);

derywat/php-processes 适用场景与选型建议

derywat/php-processes 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 26 次下载、GitHub Stars 达 0, 最近一次更新时间为 2025 年 12 月 11 日, 在 PHP 生态内属于活跃度较高的组件。

我们在过去多个企业项目中使用过 derywat/php-processes 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 derywat/php-processes 我们能提供哪些服务?
定制开发 / 二次开发

基于 derywat/php-processes 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

  • 总下载量: 26
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 0
  • 点击次数: 16
  • 依赖项目数: 1
  • 推荐数: 0

GitHub 信息

  • Stars: 0
  • Watchers: 0
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2025-12-11