Beefy Boxes and Bandwidth Generously Provided by pair Networks
"be consistent"
 
PerlMonks  

Many children, but never more than a fixed number at once.

by Anonymous Monk
on Mar 09, 2018 at 14:45 UTC ( #1210569=perlquestion: print w/replies, xml ) Need Help??
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.

Replies are listed 'Best First'.
Re^3: Many children, but never more than a fixed number at once.
by haukex (Abbot) on Mar 10, 2018 at 11:19 UTC

    Hello Paolo and welcome to the Monastery. I would recommend registering an account, and also posting new questions as new posts in Seekers of Perl Wisdom, since they will get more visibility there (the post you are replying to is ~16 years old). I have considered this node for moving there, so you don't need to repost it.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://1210569]
Approved by Athanasius
help
Chatterbox?
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others making s'mores by the fire in the courtyard of the Monastery: (5)
As of 2018-06-18 23:25 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    Should cpanminus be part of the standard Perl release?



    Results (111 votes). Check out past polls.

    Notices?