Giter Club home page Giter Club logo

timers's Introduction

Timers

Gem Version Build Status Code Climate Coverage Status

Ruby timer collections. Schedule several procs to fire after configurable delays or at periodic intervals.

This gem is especially useful when you are faced with an API that accepts a single timeout but you want to run multiple timers on top of it. An example of such a library is nio4r, a cross-platform Ruby library for using system calls like epoll and kqueue.

Usage

Create a new timer group with Timers::Group.new:

require 'timers'

timers = Timers::Group.new

Schedule a proc to run after 5 seconds with Timers::Group#after:

five_second_timer = timers.after(5) { puts "Take five" }

The five_second_timer variable is now bound to a Timers::Timer object. To cancel a timer, use Timers::Timer#cancel

Once you've scheduled a timer, you can wait until the next timer fires with Timers::Group#wait:

# Waits 5 seconds
timers.wait

# The script will now print "Take five"

You can schedule a block to run periodically with Timers::Group#every:

every_five_seconds = timers.every(5) { puts "Another 5 seconds" }

loop { timers.wait }

If you'd like another method to do the waiting for you, e.g. Kernel.select, you can use Timers::Group#wait_interval to obtain the amount of time to wait. When a timeout is encountered, you can fire all pending timers with Timers::Group#fire:

loop do
  interval = timers.wait_interval
  ready_readers, ready_writers = select readers, writers, nil, interval

  if ready_readers || ready_writers
    # Handle IO
    ...
  else
    # Timeout!
    timers.fire
  end
end

You can also pause and continue individual timers, or all timers:

paused_timer = timers.every(5) { puts "I was paused" }

paused_timer.pause
10.times { timers.wait } # will not fire paused timer

paused_timer.resume
10.times { timers.wait } # will fire timer

timers.pause
10.times { timers.wait } # will not fire any timers

timers.resume
10.times { timers.wait } # will fire all timers

License

Copyright (c) 2014 Celluloid timers project developers (given in the file AUTHORS.md).

Distributed under the MIT License. See LICENSE file for further details.

timers's People

Contributors

tarcieri avatar ioquatix avatar copiousfreetime avatar skinnyjames avatar chuckremes avatar deadprogram avatar larrylv avatar godfat avatar brunoenten avatar dim avatar jc00ke avatar klaustrainer avatar lavirthewhiolet avatar mike-bourgeous avatar ryanlecompte avatar nevans avatar

Watchers

James Cloos avatar york avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.