UV::Async - Async notification handles in libuv
#!/usr/bin/env perl use strict; use warnings; use UV; # A new handle will be initialized against the default loop my $async = UV::Async->new(); # Use a different loop my $loop = UV::Loop->new(); # non-default loop my $async = UV::Async->new( loop => $loop, on_close => sub {say "close!"}, on_async => sub {say "async!"}, ); # setup the async callback: $async->on(async => sub {say "We're IDLING!!!"}); # trigger the async callback $async->send();
This module provides an interface to libuv's async handle.
Async handles store a callback to be invoked when requested by some possibly-asynchronous activity, such as in a signal handler or OS-level thread. They are generally not that useful from Perl code, but are included for completeness in case a situation arises for them.
UV::Async inherits all events from UV::Handle and also makes the following extra events available.
$handle->on(async => sub { my $invocant = shift; say "We were invoked!"}); my $count = 0; $handle->on("async", sub { my $invocant = shift; # the handle instance this event fired on if (++$count > 2) { say "We were invoked twice. stopping!"; $invocant->stop(); } });
When the event loop runs and the async is invoked, this event will be fired.
UV::Async inherits all methods from UV::Handle and also makes the following extra methods available.
my $async = UV::Async->new(); # Or tell it what loop to initialize against my $async = UV::Async->new( loop => $loop, on_close => sub {say "close!"}, on_async => sub {say "async!"}, );
This constructor method creates a new UV::Async object and initializes the handle with the given UV::Loop. If no UV::Loop is provided, then the "default_loop" in UV::Loop is assumed.
$async->send();
The send method schedules the event loop to wake up and invoke the async callback.
Paul Evans <leonerd@leonerd.org.uk>
This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself.
To install UV, copy and paste the appropriate command in to your terminal.
cpanm
cpanm UV
CPAN shell
perl -MCPAN -e shell install UV
For more information on module installation, please visit the detailed CPAN module installation guide.