如何在RoR中访问environment.rb中定义的变量?

时间:2021-05-31 23:36:55

I want to create a thread object in environment.rb and use it in some other action of some controller.

我想在environment.rb中创建一个线程对象,并在某些控制器的其他操作中使用它。

How should I do it?

我该怎么办?

Thanks in advance.

提前致谢。


Actually, I want three processes to be running perpetually which are fetching some data and storing it in database. That's why I am using threads. Is there any other way to do so?

实际上,我希望永久运行三个进程,这些进程正在获取一些数据并将其存储在数据库中。这就是我使用线程的原因。有没有其他方法可以这样做?

3 个解决方案

#1


To answer your initial question, constants declared in environment.rb are available throughout the entire codebase. Avoid doing so if you can, though; this can become configuration spaghetti pretty quickly.

为了回答您的初始问题,在整个代码库中可以使用environment.rb中声明的常量。但是,如果可以,请避免这样做;这可以很快成为配置意大利面条。

More broadly, although Rails has been (from what I understand) thread-safe since version 2.2, threads are still quite uncommon - particularly in MRI - as a way to provide concurrent operation, and MRI's green threads are anyway not particularly helpful. Consider using a message queue like Starling that spins up other Ruby processes to perform asynchronous work.

更广泛地说,尽管Rails(从我的理解)自2.2版以来一直是线程安全的,但线程仍然非常罕见 - 特别是在MRI中 - 作为提供并发操作的一种方式,而且MRI的绿色线程无论如何都不是特别有用。考虑使用像Starling这样的消息队列来旋转其他Ruby进程来执行异步工作。

#2


Further to what Brian says, consider using an initializer (put in config/initializers to have it executed). I think it makes the intent more clear than using environment.rb.

除了Brian所说的,考虑使用初始化器(放入config / initializers来执行它)。我认为它比使用environment.rb更明确。

#3


Be very careful with this. To the best of my knowledge, rails is not thread-safe. And trying to use threads safely in the face of all the magic (excuse me, "meta programming") it does sounds risky as all get out.

要非常小心。据我所知,rails不是线程安全的。并试图在面对所有魔法时安全地使用线程(对不起,“元编程”),它确实听起来有风险,因为所有人都会离开。

Why do you want a thread object anyway?

为什么你还想要一个线程对象?

In response to the comment, saying rails is thread safe might not mean as much as you think. I's certainly be leery of counting on it at if I didn't need to.

在回应评论时,说rails是线程安全的可能并不像你想象的那么多。如果我不需要的话,我肯定会对它依赖它。

#1


To answer your initial question, constants declared in environment.rb are available throughout the entire codebase. Avoid doing so if you can, though; this can become configuration spaghetti pretty quickly.

为了回答您的初始问题,在整个代码库中可以使用environment.rb中声明的常量。但是,如果可以,请避免这样做;这可以很快成为配置意大利面条。

More broadly, although Rails has been (from what I understand) thread-safe since version 2.2, threads are still quite uncommon - particularly in MRI - as a way to provide concurrent operation, and MRI's green threads are anyway not particularly helpful. Consider using a message queue like Starling that spins up other Ruby processes to perform asynchronous work.

更广泛地说,尽管Rails(从我的理解)自2.2版以来一直是线程安全的,但线程仍然非常罕见 - 特别是在MRI中 - 作为提供并发操作的一种方式,而且MRI的绿色线程无论如何都不是特别有用。考虑使用像Starling这样的消息队列来旋转其他Ruby进程来执行异步工作。

#2


Further to what Brian says, consider using an initializer (put in config/initializers to have it executed). I think it makes the intent more clear than using environment.rb.

除了Brian所说的,考虑使用初始化器(放入config / initializers来执行它)。我认为它比使用environment.rb更明确。

#3


Be very careful with this. To the best of my knowledge, rails is not thread-safe. And trying to use threads safely in the face of all the magic (excuse me, "meta programming") it does sounds risky as all get out.

要非常小心。据我所知,rails不是线程安全的。并试图在面对所有魔法时安全地使用线程(对不起,“元编程”),它确实听起来有风险,因为所有人都会离开。

Why do you want a thread object anyway?

为什么你还想要一个线程对象?

In response to the comment, saying rails is thread safe might not mean as much as you think. I's certainly be leery of counting on it at if I didn't need to.

在回应评论时,说rails是线程安全的可能并不像你想象的那么多。如果我不需要的话,我肯定会对它依赖它。