Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:
Hi monks, I would like to go further: a tcp producer/consumer + fork. And run no more than n processes at the same time on the client side.
### tcp server use DBI; use IO::Socket::INET; ### flush after every write $|= 1; ### infinite loop while(1) { my($socket, $client_socket); my($peeraddress, $peerport); my($row, $data); my @processes= ('one', 'two', 'three', 'four', 'five', 'six', 'seven +', 'eight', 'nine', 'ten'); # creating object interface of IO::Socket::INET modules which intern +ally does # socket creation, binding and listening at the specified port addre +ss. $socket = new IO::Socket::INET ( LocalHost=> '127.0.0.1', LocalPort=> '5000', Proto=> 'tcp', Listen=> 5, Reuse=> 1 ) or die "ERROR in Socket creation: $!\n"; print "I am waiting clients to connect on port 5000.\n"; while($row= shift(@processes)) { $client_socket = $socket->accept(); $peeraddress = $client_socket->peerhost(); print "Sending $row to $peeraddress:$peerport)... "; # write to the newly accepted client. print $client_socket "$row\n"; # read from the newly accepted client. $data = <$client_socket>; chomp($data); print "got $data from client.\n"; $client_socket->close(); } $socket->close(); }
### tcp client use strict; use warnings 'all'; sub sfork($&) { my($max, $code)= @_; foreach my $c (1..$max) { wait unless $c<=$max; die "Cannot fork: $!\n" unless defined(my $pid= fork); exit $code->($c) unless $pid; } 1 until -1 == wait; } sfork 2, sub { sub getFromProducer { use IO::Socket::INET; my($socket, $data); $socket= new IO::Socket::INET ( PeerHost=> '127.0.0.1', PeerPort=> '5000', Proto=> 'tcp' ) or die "ERROR in Socket creation: $!\n"; $socket->autoflush(1); $data= <$socket>; chomp($data); $socket->close(); return $data; } my $data= &getFromProducer; print "($$) Got $data from procucer.\n"; }
This is not exactly what I would like to obtain, any suggestion is well appreciated.
I would like the client to iterate asking, until the end of the list.
A new monk is in search of wisdom. Paolo F.2018-03-12 Athanasius promoted to root node; formerly a reply to Re: Many children, but never more than a fixed number at once.
Back to
Seekers of Perl Wisdom