Sha256: 3cd279506adf585caafc5127ac7312acc3bb9fa1b746c3cbbc31a3eb9a4702a3

Contents?: true

Size: 1.79 KB

Versions: 12

Compression:

Stored size: 1.79 KB

Contents

Timers
======
[![Build Status](https://secure.travis-ci.org/tarcieri/timers.png?branch=master)](http://travis-ci.org/tarcieri/timers)

Pure 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](https://github.com/tarcieri/nio4r), a cross-platform
Ruby library for using system calls like epoll and kqueue.

Usage
-----

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

```ruby
require 'timers'

timers = Timers.new
```

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

```ruby
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#wait`:

```ruby
# Waits 5 seconds
timers.wait

# The script will now print "Take five"
```

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

```ruby
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#wait_interval` to obtain the amount of time to wait. When
a timeout is encountered, you can fire all pending timers with `Timers#fire`:

```ruby
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
```

License
-------

Copyright (c) 2012 Tony Arcieri. Distributed under the MIT License. See
LICENSE for further details.

Version data entries

12 entries across 12 versions & 3 rubygems

Version Path
vagrant-tiktalik-0.0.3 vendor/bundle/ruby/2.0.0/gems/timers-1.1.0/README.md
sidekiq-statsd-0.1.1 vendor/ruby/1.9.1/gems/timers-1.1.0/README.md
sidekiq-statsd-0.1.0 vendor/ruby/1.9.1/gems/timers-1.1.0/README.md
timers-1.1.0 README.md
timers-1.0.2 README.md
timers-1.0.1 README.md
timers-1.0.0 README.md
timers-1.0.0.pre4 README.md
timers-1.0.0.pre3 README.md
timers-1.0.0.pre2 README.md
timers-1.0.0.pre README.md
timers-0.0.0 README.md