Time limited computation in Ruby

I want to run a task in Ruby for up to 10 seconds and kill this task if it took longer. This is done to prevent the external process from freezing. What is the best way to implement this? In particular, how to write the for_up_to_10_seconds function below?

 loop do for_up_to_10_seconds do # something end end 
+4
source share
1 answer

The Timeout class from the standard library is what you are looking for: http://www.ruby-doc.org/core/classes/Timeout.html

 loop do Timeout.timeout(10) do # something end end 
+7
source

Source: https://habr.com/ru/post/1301416/


All Articles