The London Perl and Raku Workshop takes place on 26th Oct 2024. If your company depends on Perl, please consider sponsoring and/or attending.

NAME

UV::Async - Async notification handles in libuv

SYNOPSIS

#!/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();

DESCRIPTION

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.

EVENTS

UV::Async inherits all events from UV::Handle and also makes the following extra events available.

async

$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.

METHODS

UV::Async inherits all methods from UV::Handle and also makes the following extra methods available.

new

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.

send

$async->send();

The send method schedules the event loop to wake up and invoke the async callback.

AUTHOR

Paul Evans <leonerd@leonerd.org.uk>

LICENSE

This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself.