Is there any notable difference between
sleep 10
and
wait_until(10)
They both seem to do the same thing: wait 10 seconds then proceed to the next step
sleep just does nothing for the specified time. wait_until takes a block. It waits until the block evaluates to true or times out. If no block is given they act the same.
Related
I'm trying to get all of a user's follower ids (75K+) without hitting the rate limit. I figured you can put a sleep method on the cursor to prevent over 15 calls per 15 minutes. Any idea how to do that? Thanks in advance. :)
I guess you are using the twitter gem for interacting with the Twitter API. There is exactly your scenario described in one of their wikis:
follower_ids = client.follower_ids('justinbieber')
begin
follower_ids.to_a
rescue Twitter::Error::TooManyRequests => error
# NOTE: Your process could go to sleep for up to 15 minutes but if you
# retry any sooner, it will almost certainly fail with the same exception.
sleep error.rate_limit.reset_in + 1
retry
end
The idea is to simply sleep an amount of time if the rate limit has been reached, then retry the API call.
If you would like to avoid the rate limiting altogether, you can take limit - 1 elements from the returned cursor every x seconds. In your case, take 15 elements, then sleep for 15 minutes. Here's an example:
follower_ids = client.follower_ids('justinbieber')
loop do
follower_ids.take(15)
break if follower_ids.last?
sleep 15 * 60 # 15 minutes
end
while looking on how to parallelize bash tasks, I've stumbles over a code like this:
for item in "${items[#]}"
do
((i=i%THREADS)); ((i++==0)) && wait
process_item $item &
done
Where process_item is some king of function/program that works with item and the THREADS var contain the maximum number of background processes that can run simultaneously.
Can someone explain to me how this works? I understand that i=i%THREADS ensures that i is between 0 and THREADS-1, and that i++==0 increments i and checks whether it is 0. But is wait bound to all sub processes? Or how does it know that is has to wait until the previous batch stopped processing?
It's an obfuscated way of writing
for item in "${items[#]}"
do
# Every THREADSth job, stop and wait for everything
# to complete.
if (( i % THREADS == 0 )); then
wait
fi
((i++))
process_item $item &
done
It also doesn't actually work terribly well. It doesn't ensure that there are always $THREADS jobs running, only that no more than $THREADS jobs are running at once.
i++==0 checks and increments, not the opposite. wait waits for all currently active child processes. So, each iteration (but the first, thanks to the ((i++==0))) first waits for the process launched by the previous iteration and launches a new process.
Is there a bash command that waits for a window to open? Right now I'm doing something along the lines of:
open-program
sleep 100 # Wait for the program to open
send-keyboard-input
Is there a way to have "send-keyboard-input" wait until open-program finishes, eliminating the sleep 100? The time always varies, sometimes it's 90 seconds, sometimes it's 50 second.
Have you tried this?
open-program && send-keyboard-input
I want to know how to execute a function after a certain amount of time has passed. The user will enter a duration, say 30 minutes, and after 30 minutes they will be given a message, along with other code being done. I am new to Ruby, and can't figure out the best way to do it.
If you don't want to block IO you can use threads:
time = gets.to_i # time in seconds
Thread.new do
sleep time
# your code here
end
Or just:
time = gets.to_i # time in seconds
sleep time
# your code here
You could look into gems like DelayedJob or Resque.
I am getting into ruby and have been using threads for a little while now with out fully understanding them. I notice that when adding a thread to an array and if I add a sleep() command as the first command the thread does not run until I do a join which is mostly what I want. So I have 2 questions.
1.Is that suppose to happen?
2.Is there a better way to do that other then the way I'm doing it. Here is a sample code that I have to show what I'm talking about.
job = Array.new
10.times do |n|
job << Thread.new do
sleep 0.001
puts "done #{n}"
end
end
#job.each do |t|
#t.join
#end
puts "End of script"
Output is
End of script
If I remove the comments output is
done 1
done 0
done 7
done 6
done 5
done 4
done 3
done 2
done 9
done 8
End of script
So I use this now but I don't understand why it does that. Sometimes I notice even doing something like `echo hi` instead of sleep does the trick.
Thanks in advance.
Timing of threads isn't a defined behavior. Once you put them to sleep, they will be put in a queue to be run later. You can't ever expect it to run one way or another.
Your main program doesn't take very long to run, so it is likely to happen to finish before your other threads get picked back up to run again. Really, when you think about it, 0.001 seconds is quite a long time to computer, so spinning off 10 threads in that time is likely to happen -- but even if it takes longer, there is no guarantee the thread will resume immediately after .001 seconds. Often there's really no guarantee it won't start before .001 seconds, either, but sleep calls usually don't end early.
When you add the join calls, you are introducing additional time into your main thread which allows the other threads time to run, so this behavior is expected.