3

AnyEvent::HTTP

Tried on Debian and Centos, both with perl 5.10

Not a sound after

perl -Mstrict -we 'use AnyEvent::HTTP; http_get "http://www.nethype.de/", sub { print $_[1] }; sleep 20'

Is there something fundamentally broken with module, or do I need more recent perl version, although I would expect complaints about it?

EDIT:

So I need event loop, is there some simple example which would demonstrate AE::HTTP usage?

mpapec
  • 50,217
  • 8
  • 67
  • 127

2 Answers2

6

The problem is that sleep, not being part of AnyEvent, doesn't execute the event loop that permits AnyEvent::HTTP to fetch asynchronously. When you block, you want to block using something AE-aware such as a condition variable.

This program creates a condition variable called $exit_wait and then makes the HTTP request. The program can continue running while the request is made and the response received.

Once the program has reached a point where it needs the information from the HTTP request, it calls recv on the condition variable. This allows the callback to trigger when the HTTP request has also completed. All it does is dump the $headers hash.

In this case I have written it so that the callback also does a send on the condition variable, which causes the program to end its recv call and continue on. Without it the program will be left in an endless wait state.

I can't help further without knowing more about your application.

use strict;
use warnings;

use AnyEvent::HTTP;
use Data::Dump;

STDOUT->autoflush;

my $exit_wait = AnyEvent->condvar;

my $handle = http_request
  GET => 'http://www.nethype.de/',
  sub {
    my ($body, $headers) = @_;
    dd $headers;
    $exit_wait->send;
  };

# Do stuff here

$exit_wait->recv;
ikegami
  • 367,544
  • 15
  • 269
  • 518
Borodin
  • 126,100
  • 9
  • 70
  • 144
  • @ikegami: Feel free to amend my question. It didn't occur to me that the `sleep` was supposed to allow the callback to run. – Borodin Apr 02 '14 at 13:00
  • @ikegami and Borodin, which event loop does this solution use, should I care, and how can I change it? – mpapec Apr 02 '14 at 13:14
  • 1
    If `AnyEvent` finds `EV` installed on the system then it will use it. Otherwise it will use its own pure Perl back end. It shouldn't be a problem to you which is used unless your program already has an event system, like a GUI system such as Wx or Gtk. It is also possible that you need something faster than the pure Perl back end, but this is very unlikely as your program will probably be network-bound. – Borodin Apr 02 '14 at 13:45
  • 1
    @mpapec, All you need to know is that `->recv` is from AE, so it will allow the event loop to run. What the loop itself is doesn't really matter (unless you want it to be a specific loop because you the rest of the application uses it). – ikegami Apr 02 '14 at 14:04
1

You might need to read a bit further than stopping at the first incomplete example. The first sentence in the description is:

This module is an AnyEvent user, you need to make sure that you use and run a supported event loop.

For documentation about the basic usage of AnyEvent* (e.g. event loops etc) see the AnyEvent documentation.

Steffen Ullrich
  • 114,247
  • 10
  • 131
  • 172
  • But the [documentation for `AnyEvent`](https://metacpan.org/module/AnyEvent) says, *" If EV is not installed, then AnyEvent will fall back to its own pure-perl implementation, which is available everywhere as it comes with AnyEvent itself"* – Borodin Apr 02 '14 at 11:27
  • 1
    @mpapec: Did this resolve your problem? I'd like to understand how as I can't get this going either. – Borodin Apr 02 '14 at 11:30
  • 1
    It would be helpful if the documentation mentioned *how* to "be an AnyEvent user". The AnyEvent documentation doesn't make it very clear. – reinierpost May 26 '16 at 19:42