if you
- just send 1 statement at a time and
- then disconnect and reconnect for the next one,
- each transmission terminate by $EOL
you could try this deamon, which seems to work for several hundred parallel client attempts. (i brute forced it up to 700..800x8kB - took 1.5 min, but the first 500 went through within 20..30sec or so - but with just 100 at a time youre done in about 10 sec. so depending on your average and peek load you have to monitor the runtime of the clients. just do several scenarios for your use and see if there is still a bottleneck...) :


#!/usr/bin/perl -Tw

use strict;
use warnings;
use IO::Socket;
use Net::hostent;
use ResourcePool;
use ResourcePool::Factory::DBI;
use ResourcePool::Command::DBI::Execute;

my $EOL = "\015\012";
sub spawn;
my $port = 6000;
my $proto = getprotobyname('tcp');
my $dsn = "DBI:mysql:DB;HOST;3306";
my $username = "xxx";
my $passwd = "yyy";

my $factory = ResourcePool::Factory::DBI->new($dsn,$username,$passwd);
my $pool = ResourcePool->new($factory, MaxTry => 3);

socket(Server, PF_INET, SOCK_STREAM, $proto) || die "socket: $!";
setsockopt(Server, SOL_SOCKET, SO_REUSEADDR, pack("l", 1)) || die "setsockopt: $!";
bind(Server, sockaddr_in($port, INADDR_ANY)) || die "bind: $!";
listen(Server,SOMAXCONN) || die "listen: $!";

my $waitedpid = 0;
my $paddr;
$SIG{CHLD} = 'IGNORE';

for ($waitedpid = 0; ($paddr = accept(CLIENT,Server)) || $waitedpid; $waitedpid = 0, close CLIENT)
{
   next if $waitedpid and not $paddr;
   my($port,$iaddr) = sockaddr_in($paddr);
   my $name = gethostbyaddr($iaddr,AF_INET);

   spawn sub {
      $|=1;
      my $line; my $clientdata = '';
      for (;;)
      {
         undef $!;
         unless (defined($line= <> ))
         {
            die $! if $!;
            last; # reached EOF
         }
         $clientdata .= $line;
         last if eof;
      }
      my $cmd = ResourcePool::Command::DBI::Execute->new();
   $pool->execute($cmd, $clientdata);
   # and here may be a possibility to send data back to the client (f.i. lastinsertid)
   # <> = "place return data here $EOL";
   };
}

sub spawn
{
   my $coderef = shift;
   unless (@_ == 0 && $coderef && ref($coderef) eq 'CODE')
   {confess "usage: spawn CODEREF";}
   my $pid;
   if (!defined($pid = fork))
   {return;} elsif ($pid) {return; # I'm the parent}
   # else I'm the child -- go spawn
   open(STDIN, "<&CLIENT") || die "can't dup client to stdin";
   open(STDOUT, ">&CLIENT") || die "can't dup client to stdout";
   exit &$coderef();
}

In reply to Re^2: how can N client.pl connect to one deamon serving mysql by saxman25
in thread how can N client.pl connect to one deamon serving mysql by Anonymous Monk

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.