Rails:是否可以在线程中初始化一些东西?

发布于 2024-10-24 23:48:15 字数 482 浏览 1 评论 0原文

在我的初始化程序之一中,我需要从 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

夜巴黎 2024-10-31 23:48:15

请您再解释一下好吗?

  • 这里的目标是什么?您正在对哈希值的内容做什么?
  • 你运行什么堆栈?

我尝试对本地计算机(OS X 10.6.7)上运行的 Rails 3.0.5/WEBrick 进行简单测试:

puts "I am in the main thread."
Thread.abort_on_exception = true
Thread.new do
  for i in 1..5
    puts "I am in a thread."
    sleep 2
  end
end
Thread.new do
  for i in 1..5
    puts "I am in another thread."
    sleep 1
  end
end

它按照我预期的方式工作:

ultramarine:ThreadTest jdc$ rails s
=> Booting WEBrick
=> Rails 3.0.5 application starting in development on http://0.0.0.0:3000
=> Call with -d to detach
=> Ctrl-C to shutdown server
I am in the main thread.
I am in a thread.
I am in another thread.
[2011-03-23 18:30:28] INFO  WEBrick 1.3.1
[2011-03-23 18:30:28] INFO  ruby 1.9.2 (2010-12-25) [x86_64-darwin10.5.0]
[2011-03-23 18:30:28] INFO  WEBrick::HTTPServer#start: pid=5802 port=3000
I am in another thread.
I am in a thread.
I am in another thread.
I am in another thread.
I am in a thread.
I am in another thread.
I am in a thread.
I am in a thread.

Will you please explain a bit more?

  • What is the goal here? What are you doing with the contents of the hashes?
  • What stack are you running?

I tried a simple test against Rails 3.0.5/WEBrick running on my local machine (OS X 10.6.7):

puts "I am in the main thread."
Thread.abort_on_exception = true
Thread.new do
  for i in 1..5
    puts "I am in a thread."
    sleep 2
  end
end
Thread.new do
  for i in 1..5
    puts "I am in another thread."
    sleep 1
  end
end

And it worked the way I expected:

ultramarine:ThreadTest jdc$ rails s
=> Booting WEBrick
=> Rails 3.0.5 application starting in development on http://0.0.0.0:3000
=> Call with -d to detach
=> Ctrl-C to shutdown server
I am in the main thread.
I am in a thread.
I am in another thread.
[2011-03-23 18:30:28] INFO  WEBrick 1.3.1
[2011-03-23 18:30:28] INFO  ruby 1.9.2 (2010-12-25) [x86_64-darwin10.5.0]
[2011-03-23 18:30:28] INFO  WEBrick::HTTPServer#start: pid=5802 port=3000
I am in another thread.
I am in a thread.
I am in another thread.
I am in another thread.
I am in a thread.
I am in another thread.
I am in a thread.
I am in a thread.
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文