Multiprocessor php with libevent

I can create a simple php websocket server with libevent, but I get stuck when I try to make it multiprocessor.

for example, this is a single processing

<?php $socket = stream_socket_server ('tcp://0.0.0.0:2000', $errno, $errstr); stream_set_blocking($socket, 0); $base = event_base_new(); $event = event_new(); event_set($event, $socket, EV_READ | EV_PERSIST, 'ev_accept', $base); event_base_set($event, $base); event_add($event); event_base_loop($base); $GLOBALS['connections'] = array(); $GLOBALS['buffers'] = array(); function ev_accept($socket, $flag, $base) { static $id = 0; $connection = stream_socket_accept($socket); stream_set_blocking($connection, 0); $id += 1; $buffer = event_buffer_new($connection, 'ev_read', NULL, 'ev_error', $id); event_buffer_base_set($buffer, $base); event_buffer_timeout_set($buffer, 30, 30); event_buffer_watermark_set($buffer, EV_READ, 0, 0xffffff); event_buffer_priority_set($buffer, 10); event_buffer_enable($buffer, EV_READ | EV_PERSIST); // we need to save both buffer and connection outside $GLOBALS['connections'][$id] = $connection; $GLOBALS['buffers'][$id] = $buffer; } function ev_error($buffer, $error, $id) { event_buffer_disable($GLOBALS['buffers'][$id], EV_READ | EV_WRITE); event_buffer_free($GLOBALS['buffers'][$id]); fclose($GLOBALS['connections'][$id]); unset($GLOBALS['buffers'][$id], $GLOBALS['connections'][$id]); } function ev_read($buffer, $id) { while ($read = event_buffer_read($buffer, 256)) { var_dump($read); } } ?> 

But when I do it in ev_read function

  function ev_read($buffer, $id) { while ($read = event_buffer_read($buffer, 256)) { $pid = pcntl_fork(); switch ($pid) { case -1: // Error die('Fork failed, your system is b0rked!'); break; case 0: // Child event_buffer_write($buffer,"asdawdasd"); exit(0); break; } } } 

it does not send data ...

So how can I create a multiprocessor php socket server?

+6
source share
1 answer

While nanoserv is a great library, it does not use libevent. Infact, the author himself wrote on his blog that he would like to convert nanoserv to use libevent at some point in time. See his blog here: http://blog.si.kz/index.php/2010/02/03/libevent-for-php

There is also a comment by Alix Axel on May 22, 11 at 12:19 regarding the same.

Update: A little more research has led me to http://phpdaemon.net/ . They seem to be using libevent to create a variety of network servers.

+5
source

Source: https://habr.com/ru/post/886260/


All Articles