Search code examples
phpmultithreadinglibevent

php libevent is multithreaded?


I have wrote simple server on php and libevent.

<?php
function process($fd, $events, $arg) {
    $conn = stream_socket_accept($fd);
    //stream_set_blocking($conn, 0);

    $read = fread($conn, 4096);

    echo "new connection " . (string)$conn . "\n";

    fwrite($conn, "The local time is " . date('r') . "\n");
    fclose($conn);

    sleep(3);
}

$server = stream_socket_server("tcp://0.0.0.0:33333", $errno, $errstr);
stream_set_blocking($server, 0);

$base = event_base_new();
$event = event_new();
event_set($event, $server, EV_READ | EV_PERSIST, "process");
event_base_set($event, $base);
event_add($event);
event_base_loop($base);

At the same time I load it in 2 different browsers http://xxx:33333/, in the first the result appears immediately, in the second in 3 or more seconds 1 - The local time is Thu, 03 Sep 2015 21:17:11 2 - The local time is Thu, 03 Sep 2015 21:17:17

I thought that libevent is multithreaded. But my example shows that not. Is it so? Or I have mistake in code?

Thanks


Solution

  • No. Libevent is not multi threaded. It handles one event at a time.