承接 derywat/php-processes 相关项目开发

从需求分析到上线部署,全程专人跟进,保证项目质量与交付效率

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

derywat/php-processes

最新稳定版本:0.1.2

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);
		}
	);

统计信息

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

GitHub 信息

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

其他信息

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