Beefy Boxes and Bandwidth Generously Provided by pair Networks
Don't ask to ask, just ask
 
PerlMonks  

Re^2: A simple web server with HTTP::Daemon and threads

by lixus (Initiate)
on Jun 23, 2009 at 10:23 UTC ( [id://773946]=note: print w/replies, xml ) Need Help??


in reply to Re: A simple web server with HTTP::Daemon and threads
in thread A simple web server with HTTP::Daemon and threads

Hello Fellows, I am really not a thread nor an OO expert but I am still wondering what is wrong with the code below.

My intention is to write a http proxy in perl. Once this is working I am going to use it to cache/serve png map tiles from e.g. www.openstreetmap.org

Why is it so slow?
Why does it stop running after a a few request?

#!/usr/bin/perl use HTTP::Daemon; use LWP::UserAgent; use threads; my $proxy = HTTP::Daemon->new( LocalPort => 3128, Listen => 20, Reuse= +>1) || die; while (my $conn = $proxy->accept) { threads->create(\&process_one_req, $conn)->detach(); } sub process_one_req { my $conn = shift; my $request = $conn->get_request; my $ua = LWP::UserAgent->new; print $request->uri,"\n"; my $response = $ua->simple_request($request); $conn->send_response($response); $conn->close; undef($conn); undef($ua); } ## end sub process_one_req

Replies are listed 'Best First'.
Re^3: A simple web server with HTTP::Daemon and threads
by mrajcok (Initiate) on Aug 10, 2011 at 15:22 UTC
    To get the code to work reliably on Windows7 (ActiveState Perl v5.12.4) I had to add a sleep of at least 15ms after the thread creation. I use 50ms for good measure:
    use Time::HiRes qw( usleep ); ... while (my $c = $d->accept) { threads->create(\&process_one_req, $c)->detach(); usleep(50_000); }
    Update: after attempting AJAX POSTs with some jQuery/JavaScript, the Perl web server code was not reliable. I seemed to loose AJAX messages. (As a workaround, I installed an AJAX error handler in the JavaScript code, which performs the same AJAX POST again (well, using setTimeout)... that worked, since I can tolerate multi-second delays -- the app updates graphs that are generated only every 4 seconds.)

    Update: after playing around with this some more, I was able to get reliable operation, and handle multiple browsers and multiple requests per connection/browser if I closed the client connection/socket in the server, and the daemon/server socket in the client:

    use HTTP::Daemon; use threads; my $d = HTTP::Daemon->new(LocalAddr => $ARGV[0], LocalPort => 80, Reuse => 1, Listen => 20) || die; print "Web Server started, server address: ", $d->sockhost(), ", serve +r port: ", $d->sockport(), "\n"; while (my $c = $d->accept) { threads->create(\&process_client_requests, $c)->detach; $c->close; # close client socket in server } sub process_client_requests { my $c = shift; $c->daemon->close; # close server socket in client while(my $r = $c->get_request) { if ($r->method eq "GET") { my $path = $r->url->path(); $c->send_file_response($path) or die $!; #or do whatever you want here } else { print "unknown method ".$r->method."\n" } } $c->close; }

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://773946]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others examining the Monastery: (7)
As of 2024-04-23 18:45 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found