I was reading some of my old posts, and came across this thread HTTP::Daemon not working in threads?.

The entire thread leads people to believe HTTP::Daemon does not work with thread, including my own reply at that time, but that is wrong.

The problem with the code in the original post, is that it didn't detach those threads. With those huge memory leaks in 5.8 threads, that is fatal. But this could be avoided by detach, in which you clearly tell Perl that you don't want to join later.

I simplified my own web server, which is HTTP::Daemon + threads, and put it here. My original code has been running for months already and there is no problem.

use HTTP::Daemon; use threads; my $webServer; my $d = HTTP::Daemon->new(LocalAddr => $ARGV[0], LocalPort => 80, Listen => 20) || die; print "Web Server started!\n"; print "Server Address: ", $d->sockhost(), "\n"; print "Server Port: ", $d->sockport(), "\n"; while (my $c = $d->accept) { threads->create(\&process_one_req, $c)->detach(); } sub process_one_req { my $c = shift; my $r = $c->get_request; if ($r) { if ($r->method eq "GET") { my $path = $r->url->path(); $c->send_file_response($path); #or do whatever you want here } } $c->close; undef($c); }

In reply to A simple web server with HTTP::Daemon and threads by pg

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.