Fork / Non Blocking HTTP Daemon
am 26.07.2006 12:50:41 von robert.hennigerHallo Newsgroup.
Ich habe vor langer Zeit ein Posting eröffnet gehabt. Es ging um das
selbe Problem. Nun stehe ich vor dem selben Problem.
http://groups.google.de/group/de.comp.lang.perl.misc/browse_ thread/thread/6=
087a0d6c32eac98/126988e0e96602c3?lnk=3Dst&q=3D&rnum=3D1&hl=3 Dde#126988e0e96=
602c3
Mein HTTP::Daemon will nicht forken!
Ich habe jetzt probiert mit Hilfe von Parallel::ForkManager ihn zum
forken zu bekommen. Jedoch ohne Erfolg.
Beispiel-Codes als ZIP und RAR:
http://www.roberthenniger.net/http_daemon.rar
http://www.roberthenniger.net/http_daemon.zip
Immer wenn es zum Forken kommt hängt sich der Server auf.
Woran liegt das?
Ich habe die Zeile mit dem Forken jetzt einmal auskommentiert und dann
läuft der Server. Ich brauche aber einen Server der meherer Tasks
gleichzeitig erledigen kann.
Wer kann mir helfen?
Code-Anregungen sind erwünscht.
Anbei noch einmal die Quellcodes zum Programm:
############################################################ ##########
## service.pl
############################################################ ##########
use strict;
use warnings;
my $VERSION =3D "0.1";
use HTTP::Daemon;
use HTTP::Status;
use HTTP::Response;
use HTML::Template;
use CGI;
use CGI::Log;
use Module::Load;
use FindBin;
use Proc::Fork;
##################################
#
use lib $FindBin::Bin.'/objects';
use Server;
use Server::ProcessRequest;
use Server::Response;
use Server::Bind;
my $init =3D {
port =3D> "90",
localaddr =3D> "127.0.0.1",
serverprotocol =3D> "tcp",
htdocs =3D> $FindBin::Bin."/htdocs/",
logdir =3D> $FindBin::Bin."/logs/",
};
my $service =3D Server->new($init);
if($service){
### Bin the standart callbacks
$service->bind();
$service->start();
}
print "finish process\n";
############################################################ ##########
## ENDE service.pl
############################################################ ##########
############################################################ ##########
## Server.pm
############################################################ ##########
package Server;
use strict;
use warnings;
sub new {
my $class =3D shift;
my $init =3D shift;
my $self =3D {};
bless($self,$class);
$self->{"RUNNING_INSTANCE"} =3D undef;
$self->configure($init);
$self->{"_BINDINGS_"} =3D Bind->new();
my @keys =3D keys(%{$self->{"PUBLIC_CONFIG"}});
if($self->{"PUBLIC_CONFIG"}->{"LocalPort"}){
my $serverinstanz =3D HTTP::Daemon->new(
LocalPort =3D> 90, LocalAddr =3D> "127.0.0.1", Proto =3D> "tcp"
);
if($serverinstanz){
$serverinstanz->product_tokens("design4fture");
$self->{"RUNNING_INSTANCE"} =3D $serverinstanz;
return $self;
}
}
}
sub bind {
my $self =3D shift;
print "testbind\n";
}
sub start {
use Parallel::ForkManager;
# Max 30 processes for parallel download
my $pm =3D new Parallel::ForkManager(30);
my $self =3D shift;
$self->{"RUNNING_SINCE"} =3D time();
$self->{"REQUESTS"} =3D 0;
print $self->{"RUNNING_INSTANCE"}->url."\n";
if($self->{"RUNNING_INSTANCE"}){
while(my $client =3D $self->{"RUNNING_INSTANCE"}->accept){
$self->{"REQUESTS"} ++;
$pm->start and next; # do the fork
print "process.....\n";
while(my $request =3D $client->get_request){
### Process Request
my $process =3D ProcessRequest->new($self->{"_BINDINGS_"});
$process->storeServerObject($self);
$process->storeClientObject($client);
$process->storeRequestObject($request);
my $r =3D $process->execute($request->url->path);
if($r ne "1" && $r){
### Maybe the e.g AOE has printed some output....
my $response =3D Response->new($client);
$response->send($r);
}
$client->close();
print "connection closed\n";
}
$pm->finish; # do the exit in the child process
}
}else{
print "server not started\n";
}
}
sub shutdown {
}
sub restart {
my $self =3D shift;
print "server restart was executed\n";
return "...rebooting server...\n";
}
sub ServerStatus {
my $self =3D shift;
my $time_running =3D time() - $self->{"RUNNING_SINCE"};
my $html =3D'
Server Information
Server started: '.$self->{"RUNNING_SINCE"}.'
Server running time: '.$time_running.'
Server handled requests:
'.$self->{"REQUESTS"}.'
';
}
sub configure {
my $self =3D shift;
my $init =3D shift;
my $public =3D {
localaddr =3D> "LocalAddr",
port =3D> "LocalPort",
serverprotocol =3D> "Proto",
};
my $privat =3D {};
if(ref($init) eq "HASH"){
my @configParam =3D keys(%{$init});
for(my $c=3D0;$c
if($public->{$configParam[$c]}){
$self->{"PUBLIC_CONFIG"}->{ $public->{$configParam[$c]}} =3D
$init->{$configParam[$c]};
}else{
$self->{"PRIVAT_CONFIG"}->{$configParam[$c]} =3D
$init->{$configParam[$c]};
}
}
return 1;
}else{
return undef;
}
}
1;
############################################################ ##########
## ENDE Server.pm
############################################################ ##########
Mit freundlichen Grüßen
Robert Henniger