AnyEvent-Beanstalk-Worker
view release on metacpan or search on metacpan
lib/AnyEvent/Beanstalk/Worker.pm view on Meta::CPAN
}
say STDERR "parsing " . $job->data;
eval {
$tx->res->dom->at("html body")->find('a[href]')
->each(sub { $self->emit(add_url => shift->{href}) });
};
return $self->finish(delete => $job->id);
});
$w->on(add_url => sub {
my ($self, $url) = @_;
return unless $url =~ /^http/;
$self->beanstalk
->put({ priority => 100,
ttr => 15,
delay => 1,
data => $url },
sub { say STDERR "URL $url added" });
});
$w->start;
AnyEvent->condvar->recv;
We've just written a simple (and impolite--should read F<robots.txt>)
web crawler.
See F<eg/web-state.pl> and F<eg/web-state-add.pl> for this example.
=head2 Introduction to event loops
I couldn't find any gentle introductions into event loops; I was going
to write one myself but realized it would probably turn into a
book. Additionally, I'm not qualified to write said book. With that
disclaimer, here is a brief, "close enough" introduction to event
loops which may help some people get an approximate mental model, good
enough to begin event programming.
An event loop can be as simple as this:
my @events = ();
my %watchers = ();
while (1) {
my $event = pop @events;
handle($event);
}
sub handle {
my $event = shift;
$_->($event) for @{$watchers{$event->{type}}};
}
The C<@events> list (or queue, since events are read as a FIFO) might
be populated asynchronously from system events, such as receiving
signals, network data, disk I/O, timers, or other sources. The
C<handle()> subroutine checks the C<%watchers> hash to see if there
are any watchers or handlers for this event and calls those
subroutines as needed. Some of these subroutines may add more events
to the event queue. Then the loop starts again.
Most of the time you never see the event loop--you just start it. For
example, most of the time when I'm programming with B<EV>, this is all
I ever see of it:
EV::run;
B<EV> receives all kinds of events from the system, but you can tell
it about more events. Then you register event I<handlers> to fire off
when a particular kind of event is received.
=head1 SEE ALSO
B<beanstalkd>, by Keith Rarick: L<http://kr.github.io/beanstalkd/>
B<AnyEvent::Beanstalk>, by Graham Barr: L<AnyEvent::Beanstalk>
B<AnyEvent>, by Marc Lehmann: L<http://anyevent.schmorp.de>
=head1 AUTHOR
Scott Wiersdorf, E<lt>scott@perlcode.orgE<gt>
=head1 COPYRIGHT AND LICENSE
Copyright (C) 2014 by Scott Wiersdorf
This library is free software; you can redistribute it and/or modify
it under the same terms as Perl itself, either Perl version 5.16.1 or,
at your option, any later version of Perl 5 you may have available.
=cut
( run in 0.559 second using v1.01-cache-2.11-cpan-39bf76dae61 )