Esempio n. 1
0
    // Socket to talk to dispatcher
    $receiver = new ZMQSocket($context, ZMQ::SOCKET_REP);
    $receiver->connect("ipc://workers.ipc");
    while (true) {
        $string = $receiver->recv();
        printf("Received request: [%s]%s", $string, PHP_EOL);
        // Do some 'work'
        sleep(1);
        // Send reply back to client
        $receiver->send("World");
    }
}
//  Launch pool of worker threads
for ($thread_nbr = 0; $thread_nbr != 5; $thread_nbr++) {
    $pid = pcntl_fork();
    if ($pid == 0) {
        worker_routine();
        exit;
    }
}
//  Prepare our context and sockets
$context = new ZMQContext();
//  Socket to talk to clients
$clients = new ZMQSocket($context, ZMQ::SOCKET_ROUTER);
$clients->bind("tcp://*:5555");
//  Socket to talk to workers
$workers = new ZMQSocket($context, ZMQ::SOCKET_DEALER);
$workers->bind("ipc://workers.ipc");
//  Connect work threads to client threads via a queue
$device = new ZMQDevice($clients, $workers);
$device->run();
Esempio n. 2
0
    {
        return ++$this->_counter;
    }
    public function getCount()
    {
        return $this->_counter;
    }
}
function my_cb_func($user_data)
{
    echo time() . " {$user_data->getName()} function called {$user_data->increment()} times\n";
    return $user_data->getcount() < 5 ? true : false;
}
try {
    $ctx = new ZMQContext(1);
    $frontend = $ctx->getSocket(ZMQ::SOCKET_SUB);
    $frontend->bind("tcp://127.0.0.1:5554");
    $frontend->setSockOpt(ZMQ::SOCKOPT_SUBSCRIBE, "");
    $frontend->setSockOpt(ZMQ::SOCKOPT_LINGER, 0);
    $backend = $ctx->getSocket(ZMQ::SOCKET_PUB);
    $backend->bind("tcp://127.0.0.1:5555");
    $backend->setSockOpt(ZMQ::SOCKOPT_LINGER, 0);
    $device = new ZMQDevice($frontend, $backend);
    // Setup callback and user data for callback
    $device->setIdleCallback('my_cb_func', 2000, new UserData('idle'));
    $device->setTimerCallback('my_cb_func', 4000, new UserData('timer'));
    $device->run();
} catch (ZMQException $e) {
    echo "Failed to run the device: " . $e->getMessage() . "\n";
    exit(1);
}
Esempio n. 3
0
 public function multi_thread_serverAction()
 {
     function worker_routine()
     {
         $context = new ZMQContext();
         $receiver = new ZMQSocket($context, ZMQ::SOCKET_REP);
         $receiver->connect("ipc://workers.ipc");
         while (true) {
             $string = $receiver->recv();
             printf("Received request: [%s]%s", $string, PHP_EOL);
             sleep(1);
             $receiver->send("World");
         }
     }
     for ($thread_nbr = 0; $thread_nbr != 5; $thread_nbr++) {
         $pid = pcntl_fork();
         if ($pid == 0) {
             worker_routine();
             exit;
         }
     }
     $context = new ZMQContext();
     $clients = new ZMQSocket($context, ZMQ::SOCKET_ROUTER);
     $clients->bind("tcp://127.0.0.1:5555");
     $workers = new ZMQSocket($context, ZMQ::SOCKET_DEALER);
     $workers->bind("ipc://workers.ipc");
     $device = new ZMQDevice($clients, $workers);
     $device->run();
 }
Esempio n. 4
0
class IdleUserData
{
    protected $_counter = 0;
    public function increment()
    {
        return $this->_counter++;
    }
}
function my_idle_func($user_data)
{
    echo "Idle function called {$user_data->increment()} times\n";
}
try {
    $ctx = new ZMQContext(1);
    $frontend = $ctx->getSocket(ZMQ::SOCKET_SUB);
    $frontend->connect("tcp://127.0.0.1:5454");
    $frontend->setSockOpt(ZMQ::SOCKOPT_SUBSCRIBE, "");
    $frontend->setSockOpt(ZMQ::SOCKOPT_LINGER, 0);
    $backend = $ctx->getSocket(ZMQ::SOCKET_PUB);
    $backend->bind("tcp://127.0.0.1:5555");
    $backend->setSockOpt(ZMQ::SOCKOPT_LINGER, 0);
    $device = new ZMQDevice($frontend, $backend);
    // Return from poll every 5 seconds if there is no activity
    $device->setIdleTimeout(5000);
    // Setup callback and user data for callback
    $device->setIdleCallback('my_idle_func', new IdleUserData());
    $device->run();
} catch (ZMQException $e) {
    echo "Failed to run the device: " . $e->getMessage() . "\n";
    exit(1);
}