in reply to Multithreading on HTTP::Daemon

Hi.

Below is the code I'm trying to make multithreaded:

#!/usr/bin/perl use HTTP::Daemon; use HTTP::Status; my $d = new HTTP::Daemon LocalPort => 1285; print "Connected as: ", $d->url, "\n"; while (my $c = $d->accept) { while (my $r = $c->get_request) { if ($r->method eq 'GET') { $url = $r->url->path; $length = length($url); $substract = $length - 1; $file = substr($url, -$substract); $c->send_file_response($file); } else { $c->send_error(RC_FORBIDDEN) } } $c->close; undef($c); }
Does anyone know where in this code I should use fork and how, so that I can make it multithreaded? (code examples, appreciated)

Thanks,
Ralph :)

Edited 15 Jul 01, 08:45 pm (PDT) by footpad

Replies are listed 'Best First'.
Re: Re: Multithreading on HTTP::Daemon
by chromatic (Archbishop) on Feb 17, 2001 at 22:41 UTC
    merlyn's WT column 23 is a good place to start. It's the second thing I read when I wanted to add pre-forking to Jellybean.

    In your case, I wouldn't rely on random port numbers from HTTP::Daemon. I'd keep a hash around of about five port numbers, and have a kid in each. Fork them off from the parent, then let the parent wait for them to finish and to die off, then fork off a new kid on the same port. If you set the Reuse flag in the HTTP::Daemon constructor, you should be okay.

    If you want to get more sophisticated, you can keep a cache of most-recently used children. But once you start getting into those things, you're just playing. For production stuff, I'd go with an actual web server. It's non-trivial to get these things correct.

Re: Re: Multithreading on HTTP::Daemon
by merlyn (Sage) on Feb 17, 2001 at 21:54 UTC