sectokia has asked for the wisdom of the Perl Monks concerning the following question:
I cannot work out why in this code the callback sub is called twice. The response goes to the client after the timer expires the first time, but then it triggers all over again. Code:
Output:use strict; use warnings; use AnyEvent::HTTPD; my $h = AnyEvent::HTTPD->new(port => 8123);; my $timer; $h->reg_cb ( '/test' => sub { my ($httpd,$req) = @_; $req->respond({ content => ['text/plain', sub { my ($data_cb) = @_; return unless $data_cb; print "Setting timer...\n"; $timer = AnyEvent->timer(after => 5, cb => sub { print "Sending response...\n"; $data_cb->("Hello"); $data_cb->(); }); }] }); }, ); my $c = AnyEvent::condvar; $c->recv();
I was expecting the sub to only be called once. But it ends up being called a second time? Is there any way to avoid this? Thanks!Setting timer... Sending response... Setting timer... Sending response...
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: AnyEvent::HTTPD -> Extra Callback after response?
by Corion (Patriarch) on Jun 11, 2021 at 05:56 UTC | |
by cavac (Prior) on Jun 11, 2021 at 06:14 UTC | |
by sectokia (Friar) on Jun 11, 2021 at 08:57 UTC | |
by cavac (Prior) on Jun 11, 2021 at 11:21 UTC | |
by sectokia (Friar) on Jun 17, 2021 at 08:47 UTC | |
|
Re: AnyEvent::HTTPD -> Extra Callback after response?
by navalned (Beadle) on Jun 12, 2021 at 03:44 UTC | |
by The_Dj (Scribe) on Jun 13, 2021 at 03:41 UTC | |
by sectokia (Friar) on Jun 17, 2021 at 08:44 UTC | |
|
Re: AnyEvent::HTTPD -> Extra Callback after response?
by sectokia (Friar) on Jan 27, 2023 at 00:17 UTC |