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.


In reply to Re^3: Constant communication between processes (timing unix sockets) by ikegami
in thread Constant communication between processes by citycrew

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.