It is not the client requesting twice. It does it even with LWP::Simple getprint. Note: I am running on windows.

The first sub is only called on on each client connect. The second sub is gets called twice for reason.

I can get around it as per below, by storing a flag to indicate I've already handled this 'request':

use strict; use warnings; use AnyEvent::HTTPD; my $h = AnyEvent::HTTPD->new(port => 8123);; my $requests = {}; my $requestCounter = 0; $h->reg_cb ( '/test' => sub { my ($httpd,$req) = @_; my $request = $requestCounter; $requestCounter++; print "Starting request $request\n"; $requests->{$request}->{'state'} = 'new'; $req->respond({ content => ['text/plain', sub { my ($data_cb) = @_; return unless $data_cb; if (($requests->{$request}->{'state'} // 0) ne 'new') { $data_cb->(); return; } $requests->{$request}->{'state'} = 'handled'; print "Setting timer for $request...\n"; $requests->{$request}->{'timer'} = AnyEvent->timer(aft +er => 3, cb => sub { print "Sending response for $request...\n"; $data_cb->("You are request $request"); $data_cb->(); delete $requests->{$request}; return; }); }] }); }, ); my $c = AnyEvent::condvar; $c->recv();

In reply to Re^2: AnyEvent::HTTPD -> Extra Callback after response? by sectokia
in thread AnyEvent::HTTPD -> Extra Callback after response? by sectokia

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.