LINUX.ORG.RU

Многозадачность с помощью неблокирующихся сокетов в Perl


0

0

Еще раз всем привет! Подскажите, мне, плиз, как реализовать данный сабж (пример какой-нибудь живой можно?)

В общем у меня массив сокетов @SOCK, и мне нужно периодически проверять, пришло ли что от них.

anonymous

Если сокетов больше N (скажем, десятки) - то лучше IO::Poll, чем select

Если сотни или больше - то лучше поискать модуль, который умеет работать с "быстрой" нотификацией для вашей системы (epoll/kqueue/что у вас там), или с AIO.

anonymous
()
Ответ на: комментарий от zwon

А можно все-таки живой пример для нескольких сокетов, а то что-то не совсем себе это представляю

anonymous
()

$sock -> autoflush(1); кажется так, это выключение буферизации вроде и потом perldoc perlipc:

           #!/usr/bin/perl -w
           use strict;
           use IO::Socket;
           my ($host, $port, $kidpid, $handle, $line);

           unless (@ARGV == 2) { die "usage: $0 host port" }
           ($host, $port) = @ARGV;

           # create a tcp connection to the specified host and port
           $handle = IO::Socket::INET->new(Proto     => "tcp",
                                           PeerAddr  => $host,
                                           PeerPort  => $port)
                  or die "can't connect to port $port on $host: $!";

           $handle->autoflush(1);              # so output gets there right away
           print STDERR "[Connected to $host:$port]\n";

           # split the program into two processes, identical twins
           die "can't fork: $!" unless defined($kidpid = fork());

           # the if{} block runs only in the parent process
           if ($kidpid) {
               # copy the socket to standard output
               while (defined ($line = <$handle>)) {
                   print STDOUT $line;
               }
               kill("TERM", $kidpid);                  # send SIGTERM to child
           }
           # the else{} block runs only in the child process
           else {
               # copy standard input to the socket
               while (defined ($line = <STDIN>)) {
                   print $handle $line;
               }
           }

       The kill function in the parent's if block is there to
       send a signal to our child process (current running in the
       else block) as soon as the remote server has closed its
       end of the connection.

       If the remote server sends data a byte at time, and you
       need that data immediately without waiting for a newline
       (which might not happen), you may wish to replace the
       while loop in the parent with the following:







21/Apr/2001            perl 5.005, patch 03                    23





PERLIPC(1)     User Contributed Perl Documentation     PERLIPC(1)


           my $byte;
           while (sysread($handle, $byte, 1) == 1) {
               print STDOUT $byte;
           }

       Making a system call for each byte you want to read is not
       very efficient (to put it mildly) but is the simplest to
       explain and works reasonably well.


ну и таким раком собсно вешаешь на все свои сокеты while (sysread($handle, $byte, 1) == 1) { bla-bla } и оно тебя 
само информирует о приходе очередного байта, если я правильно понял, что тебе нужно.

vilfred ☆☆
()
Вы не можете добавлять комментарии в эту тему. Тема перемещена в архив.