简体   繁体   English

Ruby类成员变量现在可以了吗?

[英]Are Ruby class member variables OK now?

Last May at Railsconf on Portland, I went to a presentation where it was argued that, in Rails, Ruby class member variables, like @@foo, are dangerous because they are inherently unthreadsafe. 去年五月,我在波特兰的Railsconf上参加了一次演讲,当时有人争论说,在Rails中,像@@ foo这样的Ruby类成员变量很危险,因为它们本质上是非线程安全的。

I researched the question afterward and I never found a link that really fleshed out the question. 之后,我研究了这个问题,但从未找到真正充实该问题的链接。 I would appreciate a pointer to a good article on Rails and threads that really goes into the class member question. 我将很高兴获得指向有关Rails和线程的好文章的指针,该文章确实涉及到类成员问题。 Also, It would be nice to know how Rail 2+ and Yarv has changed things in the regard. 另外,很高兴知道Rail 2+和Yarv如何改变了这一方面。

Edit: 编辑:

Perhaps my memory of the presentation is foggy but what I remember was that @@foo had problems beyond the usual caveats that any shared variable access must be strictly controlled. 也许我对演示文稿的记忆是模糊的,但我记得@@ foo遇到的问题超出了通常的警告,即必须严格控制任何共享变量的访问。 I know that there were memory leaks in the Ruby code itself that were fixed a little while ago. 我知道Ruby代码本身存在内存泄漏,该漏洞已在不久前修复。 I'm looking for article links on Ruby shared variables and multitasking, the more in-depth, the better. 我正在寻找有关Ruby共享变量和多任务的文章链接,越深入越好。 *Currently I don't use class variable for anything because of this but it would be nice to be able use them in certain situations. *目前,由于这个原因,我什么也不使用类变量,但是在某些情况下能够使用它们会很好。

Any shared mutable state is inherently thread-unsafe. 任何共享的可变状态本质上都是线程不安全的。 You need to lock all accesses to ensure that everything's safe, and ensure that everything is re-entrant. 您需要锁定所有访问权限,以确保所有内容都是安全的,并确保所有内容都是可重入的。 @@foo is particularly bad because it's harder to audit code because any subclass can be accessing the variable. @@foo尤其糟糕,因为更难审核代码,因为任何子类都可以访问该变量。 Rails 2+ just "solved" the problem by auditing everything and making sure that mutexes and other synchronisation primitives were used where necessary. Rails 2+通过审核所有内容并确保在必要时使用了互斥锁和其他同步原语,才“解决了”问题。

I think they're as OK as they ever were, but still to be used with caution in a Rails environment where the class may be loaded multiple times (once per mongrel, for example, if you use mongrel) so the class member variable could vary independently within those processes. 我认为它们像以前一样还可以,但是在Rails环境中仍要谨慎使用,在该环境中类可能会多次加载(例如,如果您使用杂种,则每个杂种一次),因此类成员变量可以在这些过程中独立变化。

I think there's a scoping change for @@ variables in Ruby 1.9 , which should probably be taken into account - we'll all be there one day. 我认为Ruby 1.9中@@变量有一个范围界定更改,可能应该考虑到这一点-我们将有一天都在那里。

Was there a particular use you had in mind? 您是否有特定用途? I thought I needed one recently, but it turned out to be a fault in my (sketchy) understanding of the topic - what I actually needed was an instance variable on the class. 我以为我最近需要一个,但是事实证明这是我对这个主题的(粗略的)理解上的错误-我真正需要的是该类上的一个实例变量。 (I was building a module to extend a class so that I could add some AR-style declarative macro goodness.) (我正在构建一个模块来扩展类,以便可以添加一些AR风格的声明性宏。)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM