in reply to Re^2: Constant communication between processes
in thread Constant communication between processes
server.pl:
#!/usr/bin/env perl use strict; use warnings; use Cwd qw( realpath ); use IO::Socket qw( AF_UNIX SOCK_STREAM SOMAXCONN ); use Path::Class qw( file ); sub done { exit(0); } $SIG{INT } = \&done; $SIG{TERM} = \&done; $SIG{HUP } = \&done; my $socket_path = file(realpath($0))->dir()->file('socket'); { my $server = IO::Socket->new( Domain => AF_UNIX, Type => SOCK_STREAM, Local => $socket_path, Listen => SOMAXCONN, ) or die("Can't create server socket: $!\n"); eval 'END { unlink $socket_path } 1' or die $@; while (my $client = $server->accept()) { # ... } die("Can't accept: $!\n"); }
client.pl:
#!/usr/bin/env perl use strict; use warnings; use Cwd qw( realpath ); use IO::Socket qw( AF_UNIX SOCK_STREAM ); use Path::Class qw( file ); use Time::HiRes qw( time ); my $socket_path = file(realpath($0))->dir()->file('socket'); my $num_connects = $ARGV[0] || 1000; my $stime = time; for (1..$num_connects) { my $client = IO::Socket->new( Domain => AF_UNIX, Type => SOCK_STREAM, Peer => $socket_path, ) or die("Can't connect to server socket: $!\n"); # ... } my $etime = time; printf("%.3fms per connection\n", ($etime-$stime)/$num_connects*1000);
On a busy web server, I get 0.199ms per connection.
I don't know how long connecting to shared memory, ensuring exclusivity and notifying the server, but I don't think it's worth the extra effort even if it is faster.
|
|---|