Rails:是否可以在线程中初始化一些东西?
在我的初始化程序之一中,我需要从 Redis 实例中获取一些哈希值。 然而,由于哈希的数量和连接的弱点,加载可能需要相当长的时间。 由于它位于初始化程序中,因此在加载所有哈希值之前应用程序不可用。
因此,我想我可以在线程中执行初始化,以便应用程序可以启动,然后哈希值将按时加载,因为它们对应用程序来说不是必需的。
我尝试过这样的事情:
REDIS = Redis.new(:host => uri.host, :port => uri.port, :password => uri.password)
STORE = {}
Thread.abort_on_exception = true
Thread.new do
REDIS.keys.each do |key|
STORE[key] = REDIS[key]
end
end
但它不起作用,并且没有错误消息:(
有什么想法吗
?
In one of the my initializer I need to fetch some hashes from a redis instance.
However due to the number of hashs and the connection weaknesses the loading can take quite a moment.
Since it's in the initializer, the application is not available until the hashes have all been loaded.
Therefore I was thinking I could perform the initialization in a thread so the application can start and then the hashes would get loaded on their on time as they are not essential to the application.
I have tried something like this:
REDIS = Redis.new(:host => uri.host, :port => uri.port, :password => uri.password)
STORE = {}
Thread.abort_on_exception = true
Thread.new do
REDIS.keys.each do |key|
STORE[key] = REDIS[key]
end
end
But it does not work and there are no error messages :(
Any ideas?
Alex
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
请您再解释一下好吗?
我尝试对本地计算机(OS X 10.6.7)上运行的 Rails 3.0.5/WEBrick 进行简单测试:
它按照我预期的方式工作:
Will you please explain a bit more?
I tried a simple test against Rails 3.0.5/WEBrick running on my local machine (OS X 10.6.7):
And it worked the way I expected: