0

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

Max P.
  • 5,579
  • 2
  • 13
  • 32
  • each php request is independent of each other, unless you're using file-based sessions, in which case you're forced into a serial mode - first hit will lock the session and prevent any other parallel requests for that user. – Marc B Sep 03 '15 at 19:30
  • but it is not php request, it is request to my server ($server = stream_socket_server("tcp://0.0.0.0:33333", $errno, $errstr)), so there are no sessions – Max P. Sep 03 '15 at 19:39

1 Answers1

0

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

Community
  • 1
  • 1
symcbean
  • 47,736
  • 6
  • 59
  • 94