walkor / channel

Interprocess communication component for workerman
137 stars 49 forks source link

Event has been triggered 4 times (one for each process) #4

Closed teocci closed 5 years ago

teocci commented 5 years ago

When I create a worker that has 4 process the onWorkerStart closure has been called 4 times. Therefore, I created 4 clients listening the event so when the event occurs it will be triggered 4 times. How can I trigger it just once??

$worker = new Worker(....);
// 4 processes
$worker->count = 4;
$worker->onWorkerStart = function()
{
    // Channel客户端连接到Channel服务端
    Channel\Client::connect('127.0.0.1', 2206);
    // 要订阅的事件名称(名称可以为任意的数字和字符串组合)
    $event_name = 'my_event';
    // 订阅某个自定义事件并注册回调,收到事件后会自动触发此回调
    Channel\Client::on($event_name, function($event_data){
        print_r($event_data);
    });
};
walkor commented 5 years ago
$worker = new Worker(....);
// 4 processes
$worker->count = 4;
$worker->onWorkerStart = function($worker)
{
    // Channel客户端连接到Channel服务端
    Channel\Client::connect('127.0.0.1', 2206);
    // 要订阅的事件名称(名称可以为任意的数字和字符串组合)
    $event_name = 'my_event';
    // 只让0号进程监听my_event事件
    if ($worker->id ==0) {
        // 订阅某个自定义事件并注册回调,收到事件后会自动触发此回调
        Channel\Client::on($event_name, function($event_data){
            print_r($event_data);
        });
    }
};

Only let Process 0 listen this event like this.

xpader commented 3 years ago

This now can use queue ability. Use Client::enqueue() to put in a message, use Client::watch() to reserve a message. One message will only have one process received.