我正在写一个Ruby程序,我有一个奇怪的问题。如何判断方法或循环是否完成?
基本上这个想法是让程序在后台不断运行。该程序每30秒检查一次我的浏览器历史记录,并将任何新的历史记录项目上载到服务器。
# client.rb
history = HistoryUploader.new(Chrome)
# Run everything
loop do
history.run
sleep 30
end
的HistoryUploader
类的重要组成部分,看起来像这样
class HistoryUploader
def run
upload_history until local.last_seen_history_item == server.last_seen_history_item
end
def upload_history
# POST batches of history items to the server
end
end
我用这个代码中看到的主要问题是,如果HistoryUploader.run
时间超过30秒才能完成(这很可能也是因为它发送多个http请求),client.rb
的外部循环将尝试再次调用run
,并且我可能会收到并发请求到服务器,这真的会让事情混淆。
有没有办法阻止run
方法被调用两次,直到它完成?