Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Fastest IPC in PHP

I wonder, which is the fastest way to send data from one process to other in PHP? The data is only a short string. Curretly I have a solution with AF_UNIX sockets developed, but benchmarks show that it takes 0.100 ms to pass the data from one process to other. I wonder, if shared memory can be any faster? However, I have no idea, how to make the other process check the shared memory regularly to detect, if there are any new data written?

Current solution:

$server = socket_create(AF_UNIX, SOCK_STREAM, 0);
socket_bind($server, '/tmp/mysock');
socket_listen($server);
while(true) {
  $r = $clients;
  if(socket_select($r, $w, $e, 5) > 0) {
    $client = socket_accept($server);
    $d = trim(socket_read($client, 256, PHP_NORMAL_READ));
    echo (microtime(true)-$d)."\n";
    socket_close($client);
  }
  flush();
}
socket_close($server);

And client:

$d = microtime(true)."\n";
$socket = socket_create(AF_UNIX, SOCK_STREAM, 0);
socket_connect($socket, '/tmp/mysock');
socket_write($socket, $d, strlen($d));
socket_close($socket);

This solution works completely, fine, but the results are like this:

0.00019216537475586
9.5129013061523E-5
0.00011920928955078
0.00011801719665527
7.6055526733398E-5

Any ideas, how to make this script faster or to develop a faster (possibly shared memory) solution?

Thanks in advance, Jonas

like image 504
flyeris Avatar asked Nov 23 '10 21:11

flyeris


1 Answers

Shared memory is the fastest way of data interchnage between processes. For synchronization use semaphores.

like image 178
Matej Kovac Avatar answered Oct 02 '22 16:10

Matej Kovac