我有一些类似以下的东西:
# myScript.rb
require 'rufus-scheduler'
def loop
"hey I am i the loop"
end
def run_schedule(url, count, method, interval)
puts "running scheduler"
scheduler = Rufus::Scheduler.new
scheduler.every interval do
loop(url, count, method)
end
end
run_schedule(url, count, method, interval)我的期望是当我运行时:
bundle exec ruby myScript.rb url, count, method, interval我看到STD的输出是基于间隔的一堆“嘿,我在循环中”。
发生的情况是,我退出到命令行提示符,并且再也看不到循环运行。
发布于 2017-02-08 05:04:31
你怎么能期望
def loop
"hey I am i the loop"
end 输出任何东西到stdout (不是STD)?它只是返回一个字符串,而不是调用print或puts...
# myScript.rb
require 'rufus-scheduler'
def _loop(u, c, m)
# "loop" is a bad name, it's a Ruby keyword, so using "_loop" instead
# it's still a bad name
p "hey I am i the loop"
p [ Time.now, [ u, c, m ] ]
# without p or puts nothing gets to stdout
end
$scheduler = Rufus::Scheduler.new
# creating a single scheduler for the whole script
# not creating a new scheduler each time run_schedule is called
def run_schedule(url, count, method, interval)
#puts "running scheduler"
#scheduler = Rufus::Scheduler.new
# commenting out...
$scheduler.every interval do
_loop(url, count, method)
end
end
#run_schedule(url, count, method, interval)
run_schedule('url', 'count', 'method', '3s')
$scheduler.join
# let the Ruby main thread join the scheduler thread so that
# the Ruby process does not exit and so scheduling may happen所以它是这样的:
"hey I am i the loop"
[2017-02-08 06:06:01 +0900, ["url", "count", "method"]]
"hey I am i the loop"
[2017-02-08 06:06:05 +0900, ["url", "count", "method"]]
"hey I am i the loop"
[2017-02-08 06:06:08 +0900, ["url", "count", "method"]]注意脚本末尾的$scheduler.join。这会阻止Ruby进程退出。由于该进程不存在,因此其中的线程(在我们的示例中是rufus调度器实例中的线程)存活并执行它们的工作。正如预期的那样,您的初始脚本只是退出,释放了它的所有资源。
https://stackoverflow.com/questions/42097927
复制相似问题