Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

Hi.

I'm using HTTP::Deamon on Windows 95 and I've just noticed, that this module is not multithreaded.
It can't handle many requests at the same time.

Is there a way of getting it to handle many requests at the same time?

Ralph :)

Replies are listed 'Best First'.
Re: Multithreading on HTTP::Daemon
by $code or die (Deacon) on Feb 17, 2001 at 07:51 UTC
    If you are using ActivePerl and you have v5.6, then fork is implimented - you might be able to modify it to fork a new process for each incoming request.

    However, if your you are expecting to handle many simultaneous requests, you should consider installing Apache which is free and quite stable on Win 95.

    $code or die
    Using perl at
    The Spiders Web
Re: Multithreading on HTTP::Daemon
by Yohimbe (Pilgrim) on Feb 17, 2001 at 13:27 UTC
    HTTP::Daemon is not meant to be a high performance server. Its really more of a test harness kind of thing. Multithreading *might* be hacked into it fairly simply... fork() works in the latest activeperl. But I question the utility of this. A perl based webserver is never going to approach even apache in performance, even with multithreading. So unless you are doing something that a high performance perl based tool would be *ideal* for, (and I lack the imagination to come up with such a project), either live with single thread, or install a real webserver.
    --
    Jay "Yohimbe" Thorne, alpha geek for UserFriendly
Re: Multithreading on HTTP::Daemon
by Anonymous Monk on Feb 17, 2001 at 20:21 UTC
    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

      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.