How to Put a Delay on a Loop in Ruby

How to put a delay on a loop in Ruby?

The 'comment' above is your answer, given the very simple direct question you have asked:

1.upto(5) do |n|
puts n
sleep 1 # second
end

It may be that you want to run a method periodically, without blocking the rest of your code. In this case, you want to use a Thread (and possibly create a mutex to ensure that two pieces of code are not attempting to modify the same data structure at the same time):

require 'thread'

items = []
one_at_a_time = Mutex.new

# Show the values every 5 seconds
Thread.new do
loop do
one_at_a_time.synchronize do
puts "Items are now: #{items.inspect}"
sleep 5
end
end
end

1000.times do
one_at_a_time.synchronize do
new_items = fetch_items_from_web
a.concat( new_items )
end
end

Execute loop n times with a sleep delay

You can keep track of the iterations count and then sleep if it's a multiple of 150:

conversations.each.with_index do |conversation, index|
customer = conversation.customer
threaded_conversation = helpscout.conversation(conversation.id)
if index % 150 == 0
sleep 90
end
end

Ruby while loop with sleep

The console output is buffered and nobody promised to IO#flush it for you:

$i = 0
$num = 3

while $i < $num do
$stdout.puts "My loop just executed"
$stdout.flush
sleep 10
$i +=1
end

How to handle loop for delayed job

First off, I assume your @employe variable should be called @employees and stores just that: several employees

You generally should only pass the id of an object to a job. The job will query the database to retrieve the employee. It does not matter if that takes a bit longer, that's what the job is here for.

Like that, you pass the major work load to the job:

# some_controller
@employees.pluck(:id).each { |id| Delayed::Job.enqueue(id) }

# generate_pdf_job.rb
class GeneratePdf < Struct.new(:id)
def perform
employee = Employee.find(id)

employee.details.each do |detail|
pdf = EmployeDetailsPdf.new(detail)
pdf.render_file(detail.id.to_s + ".pdf")
end
end
end

How to creat a sleep loop in Ruby?

This would do it, sleep act as wait,

for i in 0..12
p i
sleep(5)
end

Execute code with random delay on Ruby

Here is one possible way - Here rand(10) is used to generate a random number between 0 and 9, and the value is used to sleep for that many seconds. sleep(n) will sleep for n seconds.

def with_delay(n)
puts "#{Time.new} Will sleep for #{n} seconds"
sleep(n)

puts "#{Time.new} Yielding now..."
yield
end

5.times do
with_delay(rand(10)) do
puts " Thanks for yielding"
end
end

Output:

2015-12-28 22:53:59 +0530 Will sleep for 1 seconds
2015-12-28 22:54:00 +0530 Yielding now...
Thanks for yielding
2015-12-28 22:54:00 +0530 Will sleep for 0 seconds
2015-12-28 22:54:00 +0530 Yielding now...
Thanks for yielding
2015-12-28 22:54:00 +0530 Will sleep for 7 seconds
2015-12-28 22:54:07 +0530 Yielding now...
Thanks for yielding
2015-12-28 22:54:07 +0530 Will sleep for 5 seconds
2015-12-28 22:54:12 +0530 Yielding now...
Thanks for yielding
2015-12-28 22:54:12 +0530 Will sleep for 2 seconds
2015-12-28 22:54:14 +0530 Yielding now...
Thanks for yielding

Tell Ruby Program to Wait some amount of time

Like this:

sleep(num_secs)

The num_secs value can be an integer or float.

Also, if you're writing this within a Rails app, or have included the ActiveSupport library in your project, you can construct longer intervals using the following convenience syntax:

sleep(4.minutes)
# or, even longer...
sleep(2.hours); sleep(3.days) # etc., etc.
# or shorter
sleep(0.5) # half a second

Delay script every X array item in Ruby

I've used something like this in the past:

SLEEP_WINDOW = 60 * 1 # minutes
csv_array.each_slice(10) do |chunk|
start_time = Time.now
chunk.each do |x|
make_api_call(x)
end
sleep_time = SLEEP_WINDOW - (Time.now - start_time)
sleep sleep_time if (sleep_time > 0)
end

The assumption is it's OK to blast through the chunks, then any remaining delay time will case the code to sleep. If the API calls took over a minute the code will loop immediately.



Related Topics



Leave a reply



Submit