UV::Idle - Idle handles in libuv


  #!/usr/bin/env perl
  use strict;
  use warnings;

  use UV;

  # A new handle will be initialized against the default loop
  my $idle = UV::Idle->new();

  # Use a different loop
  my $loop = UV::Loop->new(); # non-default loop
  my $idle = UV::Idle->new(
    loop => $loop,
    on_alloc => sub {say "alloc!"},
    on_close => sub {say "close!"},
    on_idle => sub {say "idle!"},

  # setup the idle callback:
  $idle->on(idle => sub {say "We're IDLING!!!"});

  # start the handle
  # or, with an explicit callback defined
  $idle->start(sub {say "override any 'idle' callback we already have"});

  # stop the check


This module provides an interface to libuv's idle handle.

Idle handles will run the given callback once per loop iteration, right before the UV::Prepare handles.

* Note: The notable difference with UV::Prepare handles is that when there are active UV::Idle handles, the loop will perform a zero timeout poll instead of blocking for i/o.


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


    $handle->on(idle => sub { my $invocant = shift; say "We are idling!"});
    my $count = 0;
    $handle->on("idle", sub {
        my $invocant = shift; # the handle instance this event fired on
        if (++$count > 2) {
            say "We've idled twice. stopping!";

When the event loop runs and the idle is ready, this event will be fired.

* Note: Despite the name, UV::Idle handles will get their callbacks called on every loop iteration, not when the loop is actually "idle".


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


    my $idle = UV::Idle->new();
    # Or tell it what loop to initialize against
    my $idle = UV::Idle->new(
        loop => $loop,
        on_alloc => sub {say "alloc!"},
        on_close => sub {say "close!"},
        on_idle => sub {say "idle!"},

This constructor method creates a new UV::Idle 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.


    # start the handle with the callback we supplied with ->on() or with no cb

    # pass a callback for the "idle" event
    $idle->start(sub {say "yay"});
    # providing the callback above completely overrides any callback previously
    # set in the ->on() method. It's equivalent to:
    $idle->on(idle => sub {say "yay"});

The start method starts the handle.



The stop method stops the handle. The callback will no longer be called.


Chase Whitener <>


Daisuke Murase <>


Copyright 2012, Daisuke Murase.

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