定制 yabx/ipc 二次开发

按需修改功能、优化性能、对接业务系统,提供一站式技术支持

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

yabx/ipc

最新稳定版本:0.1.0

Composer 安装命令:

composer require yabx/ipc

包简介

Simple IPC implementation for PHP based on shared memory

README 文档

README

Simple IPC implementation for PHP

Install

composer require yabx/ipc

Basic usage

<?php

use Yabx\Ipc\Ipc;
use Yabx\Ipc\Message;

require __DIR__ . '/vendor/autoload.php';

// Initialize IPC with current process ID "pr1"
$ipc = new Ipc('pr1');

// Sending payload to process ID "pr2" 
$ipc->send('pr2', ['foo' => 'baz']);

while(true) {

    // Do some things

    // Processing incoming messages from other processes
    $ipc->processMessages(function(Message $message) {
        print_r($message->getPayload());
    });

    // take a short break to reduce CPU usage
    $ipc->usleep();

}

Calling methods between processes

Contents of master.php

<?php

use Yabx\Ipc\Ipc;

require __DIR__ . '/vendor/autoload.php';

$ipc = new Ipc('master');

// Sync call
$status = $ipc->call('worker', 'get_status');

// Display result
echo 'STATUS: ' . print_r($status, 1) . PHP_EOL;

// Async call
$ipc->callAsync('worker', 'plus', [1, 2], function(int $result) {
    // Display result ans exit
    echo '1 + 2 = ' . $result . PHP_EOL;
    exit;
});

while(true) {

    // Processing messages
    $ipc->processMessages();
    
    // take a short break to reduce CPU usage
    $ipc->usleep();

}

Contents of worker.php

<?php

use Yabx\Ipc\Ipc;

require __DIR__ . '/vendor/autoload.php';

$ipc = new Ipc('worker');

$ipc->setMethod('get_status', function() {
    return [
        'status' => 'OK',
        'progress' => '50%'
    ];
});

$ipc->setMethod('plus', function(int $a, int $b) {
    return $a + $b;
});

while(true) {

    // Do some work

    // Processing incoming messages from other processes
    $ipc->processMessages();

    // take a short break to reduce CPU usage
    $ipc->usleep();

}

API

// Ipc class constructor
$ipc = new Ipc(string $processId);

// Build Message with $payload and send to $id
$ipc->send(string $id, mixed $payload): bool;

// Call synchronously $method(...$args) on $id process
$ipc->call(string $id, string $method, array $args = [], int $timeout = 30): mixed

// Call asynchronously $method(...$args) on $id process
// If $callback is defined, Result will be passed to $callback(mixed $result)
$ipc->callAsync(string $id, string $method, array $args = [], ?callable $callback = null): void

// Send raw Message
$ipc->sendMessage(Message $message): bool

// Process incoming messages
// If $callback is defined it will be called as $callback(Message $message)
$ipc->processMessages(?callable $callback = null): void

// Set method (makes it callable from other processes)
$ipc->setMethod(string $method, callable $callback): void

// Set listener to process SomePayload::class (example) messages
$ipc->setListener(SomePayload::class, function(SomePayload $payload, Message $message) { ... }): void

// Remove listener
$ipc->removeListener(string $method): void

// Set listener to process all incoming messages
$ipc->setMessageListener(callable $callback): void
$ipc->setMessageListener(function(Message $message) { ... }): void

// Sleep (0.1 sec by default)
$ipc->usleep(): void

// Changes usleep time (default: 100000 = 0.1 sec), in microseconds
Ipc::setUsleep(int $usleep): void

// Changes IPC files store path (default: /dev/shm/ipc-php)
Ipc::setIpcPath(string $ipcPath): void

统计信息

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

GitHub 信息

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

其他信息

  • 授权协议: MIT
  • 更新时间: 2022-10-27

承接程序开发

PHP开发

VUE

Vue开发

前端开发

小程序开发

公众号开发

系统定制

数据库设计

云部署

网站建设

安全加固