Rails 引擎线程安全 - ActiveResource [英] Rails Engine Thread Safety - ActiveResource

查看:56
本文介绍了Rails 引擎线程安全 - ActiveResource的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的 Rails 3.1 应用使用了一个引擎,我想知道对这个引擎的访问是否是线程安全的.

My Rails 3.1 app uses an engine and I want to know if access to this engine is threadsafe.

我在引擎中有/lib/mymodule.rb,它看起来像这样:

I have /lib/mymodule.rb in the engine and it looks something like this:

module MyModule
  def self.my_method()
    begin
       data = WebResource.find(:all) # Where WebResource < ActiveResource::Base
    rescue
       data = nil
    end

    return data
  end
end

然后在我的视图/控制器中,我像这样调用这个方法:

Then in my views/controllers, I call this method like this:

MyModule::WebResource.headers[:some_id] = cookies[:some_id]
MyModule::my_method()

在我的主应用程序中,我有线程安全!配置选项集.我知道线程安全!启用后,每个控制器对于每个请求都存在于它自己的线程中.

In my main app, I have the threadsafe! configuration option set. I know that with threadsafe! enabled, each Controller lives in it's own thread for each request.

但是,这个模块线程安全吗?我怀疑所有请求都只有这个模块的一个副本,因此它本质上不是线程安全的,并且需要使用诸如互斥锁之类的东西手动同步.具体来说,我有代码为 ActiveResource 类 WebResource 的 HTTP 请求 outside 设置标头.这会导致线程问题吗?

However, is this Module threadsafe? I suspect that there is only one copy of this module for all requests, so it is not inherently thread safe, and requires manual synchronization using something like a Mutex. Specifically, I have code that sets the header for the HTTP request outside of the ActiveResource class WebResource. Could this cause a threading issue?

推荐答案

它是否是线程安全的取决于你在这个方法中做了什么.如果它没有触及任何类变量,那么它就是线程安全的.

It will depend on what you do inside this method as to whether it is thread safe. If it touches no class variables, then it is thread safe.

如果它在类级别存储或设置信息,并假设在它再次使用之前没有其他方法会触及该信息,那么它就不是线程安全的.

If it stores or sets information at the class level and assumes that no other method is going to touch that information before it uses it again, then it is not thread safe.

这篇关于Rails 引擎线程安全 - ActiveResource的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆