C
Caleb Tennis
http://rubyforge.org/projects/posixtimer/
Here's my first RubyForge release, a small utility that wraps around the POSIX
timer API. This is only available for systems which implement timer_create()
calls - Linux being where I've tested it on.
It's very simple: create a new timer, arm it, and after a certain number of
seconds it will trigger a signal to your application:
timer = Posix::Timer.new
timer.arm(10.0)
# do some stuff for 10 seconds
10 seconds later => SignalException
You can specify a different signal than the default SIGALRM:
Posix::Timer.new("USR1")
or you can pass a block to the initializer and have it executed upon timeout:
a = Posix::Timer.new do
puts "hi"
raise MyCustomException
end
a.arm(10.0)
# 10 seconds later => MyCustomException
Other features:
a.disarm # disarm the timer
a.armed? # true or false
a.timeout # return number of seconds until timer expires
#
# Caveats
#
#
# The Posix::Timer instance uses POSIX signals to trigger
# the timeouts. This may conflict with other signals you wish to
# use in your application.
#
# Passing a block to the constructor will perform a Signal.trap on
# the signal. This may override an existing Signal.trap. In a future
# version I intend to rollback the previous trap context.
#
# Multiple timers using the same signal may conflict.
Here's my first RubyForge release, a small utility that wraps around the POSIX
timer API. This is only available for systems which implement timer_create()
calls - Linux being where I've tested it on.
It's very simple: create a new timer, arm it, and after a certain number of
seconds it will trigger a signal to your application:
timer = Posix::Timer.new
timer.arm(10.0)
# do some stuff for 10 seconds
10 seconds later => SignalException
You can specify a different signal than the default SIGALRM:
Posix::Timer.new("USR1")
or you can pass a block to the initializer and have it executed upon timeout:
a = Posix::Timer.new do
puts "hi"
raise MyCustomException
end
a.arm(10.0)
# 10 seconds later => MyCustomException
Other features:
a.disarm # disarm the timer
a.armed? # true or false
a.timeout # return number of seconds until timer expires
#
# Caveats
#
#
# The Posix::Timer instance uses POSIX signals to trigger
# the timeouts. This may conflict with other signals you wish to
# use in your application.
#
# Passing a block to the constructor will perform a Signal.trap on
# the signal. This may override an existing Signal.trap. In a future
# version I intend to rollback the previous trap context.
#
# Multiple timers using the same signal may conflict.