class PSched::Operation
Implements a recurring operation. @example General usage. Note the `sleep` call.
op = PSched::Operation.new(0.5) op.start(10) do |i| puts "Ping #{i}" end Signal.trap("SIGINT") do print "Stopping recurring process..." op.stop puts "done!" end sleep(0.2) while op.active? # THIS IS IMPORTANT!!!
@author Paolo Bosetti
Attributes
@!attribute [r] tet
The Task Execution Time
@!attribute [r] step
The time step in seconds
@!attribute [r] start_time
The time at the beginning of the current Operation
@!attribute [r] tet
The Task Execution Time
@!attribute [r] step
The time step in seconds
@!attribute [r] start_time
The time at the beginning of the current Operation
@!attribute [rw] strict_timing
If true, raise a {RealTimeError} when `TET >= step` (default to `false`)
@!attribute [r] tet
The Task Execution Time
@!attribute [r] step
The time step in seconds
@!attribute [r] start_time
The time at the beginning of the current Operation
Public Class Methods
Initializer @param [Numeric] step the timestep in seconds
# File lib/psched.rb, line 97 def initialize(step) @step = step @active = false @lock = false @strict_timing = false @tet = 0 end
Changes scheduling priority of the current process @param [Fixnum] nice sets the nice level (-20..+20) @todo better return message and error management
# File lib/psched.rb, line 77 def self.prioritize(nice = -20) result = PSched.setpriority(:prio_process,0,nice) puts "Setting max priority: #{result == 0 ? 'success' : 'failure (missing sudo?)'}" end
Public Instance Methods
Tells id the recurring operation is active or not. @return [Boolean] the status of the recurring alarm operation
# File lib/psched.rb, line 122 def active?; @active; end
Updates scheduling of pending alarms. @note Usually, there's no need to call this, since {#step=} automatically
calls it after having set the +@step+ attribute.
# File lib/psched.rb, line 115 def schedule usecs = @step * 1E6 PSched::ualarm(usecs, usecs) end
Starts a recurring operation, described by the passed block. If the block returns the symbol :stop, the recurrin operation gets disabled. @param [Fixnum,nil] n_iter the maximum number of iterations.
If nil it loops indefinitedly
@yieldparam [Fixnum] i the number of elapsed iterations @yieldparam [Float] t the time elapsed since the beginning of current Operation
@yieldparam [Float] tet the Task Execution Time of previous step @raise [ArgumentError] unless a block is given @raise [RealTimeError] if TET exceeds @step
# File lib/psched.rb, line 134 def start(n_iter=nil) raise ArgumentError, "Need a block!" unless block_given? @active = true i = 0 @start_time = Time.now Signal.trap(:ALRM) do # If there is still a pending step, raises an error containing # information about the CURRENT step if @lock then if @strict_timing @lock = false raise RealTimeError.new({:tet => @tet, :step => @step, :i => i, :time => Time.now}) end else start = Time.now @lock = true result = yield(i, Time.now - @start_time, @tet) i += 1 self.stop if (n_iter and i >= n_iter) self.stop if (result.kind_of? Symbol and result == :stop) @tet = Time.now - start @lock = false end end self.schedule end
Sets the time step (in seconds) and reschedule pending alarms. @param [Numeric] secs Timestep in seconds
# File lib/psched.rb, line 107 def step=(secs) @step = secs self.schedule end
Stops the recurring process by resetting alarm and disabling management of `SIGALRM` signal.
# File lib/psched.rb, line 163 def stop PSched::ualarm(0, 0) Signal.trap(:ALRM, "DEFAULT") @active = false @lock = false end