Ruby sleep or delay less than a second?
sleep(1.0/24.0)
As to your follow up question if that's the best way: No, you could get not-so-smooth framerates because the rendering of each frame might not take the same amount of time.
You could try one of these solutions:
- Use a timer which fires 24 times a second with the drawing code.
- Create as many frames as possible, create the motion based on the time passed, not per frame.
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
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
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
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
It's possible wait for a variable in Ruby, (without sleep)?
What about promises? Could it be what you are looking for?
https://ruby-concurrency.github.io/concurrent-ruby/1.1.5/Concurrent/Promise.html
From the docs:
Promises are similar to futures and share many of the same behaviours.
Promises are far more robust, however. Promises can be chained in a
tree structure where each promise may have zero or more children.
Promises are chained using the then method. The result of a call to
then is always another promise. Promises are resolved asynchronously
(with respect to the main thread) but in a strict order: parents are
guaranteed to be resolved before their children, children before their
younger siblings. The then method takes two parameters: an optional
block to be executed upon parent resolution and an optional callable
to be executed upon parent failure. The result of each promise is
passed to each of its children upon resolution. When a promise is
rejected all its children will be summarily rejected and will receive
the reason.
Also you can see this guide:
https://medium.com/@gauravbasti2006/lets-keep-our-promise-in-ruby-e45925182fdc
ping function every x amount seconds
If you simply sleep
for a constant amount of time as suggested in other answers, the error will contaminate as it keeps running, and will not be accurate. In fact, each iteration would take longer than the given interval.
The answer shown below adjusts the lag each time per iteration.
module Kernel
def tick_every sec, &pr
Thread.new do loop do
pr.call
t = Time.now.to_f
frac = t.modulo(sec.to_f)
sleep(sec - frac)
end end
end
end
thread = tick_every(2) do
puts "foo"
end
...
some_other_tasks
...
thread.kill
Related Topics
Error: SASS Installation for Windows
Openssl::Ssl::Sslerror: Ssl_Connect Returned=1 Errno=0 State=Unknown State: Unknown Protocol
Add "# Coding: Utf-8" to All Files
Rvm Is Not a Function, Selecting Rubies with 'Rvm Use ...' Will Not Work
Ruby on Rails: How to Run Things in the Background
Bundle Failing - Can't Find the Postgresql Client Library (Libpq)
Warning: Can't Verify Csrf Token Authenticity in Case of API Development
How to Uninstall Ruby from /Usr/Local
Escaping Single and Double Quotes in a String in Ruby
Prevent Rails Test from Deleting Seed Data
Running into Smtp Error When Trying to Send Email in Ror App
Differencebetween <%= ... %> and <% ... %> in Ruby on Rails
Ruby/Rails: Converting a Date to a Unix Timestamp
Installing Cocoapods: No Response
Convert a .Doc or .Pdf to an Image and Display a Thumbnail in Ruby