Ruby 的 stdlib Logger 类是线程安全的吗?
简而言之,Ruby 中的标准库 Logger 类是线程安全的吗?谷歌出现的唯一有用信息是论坛上有人说它“似乎”是线程安全的。而且我不想花时间测试记录器来试图弄清楚它是否有效。
目前我使用 log4r 它是线程安全的,但如果标准库已经这样做了,那就太过分了它。
In short, is the standard library Logger class in Ruby thread-safe? Only useful info Google turned up was someone on a forum saying it "seems" thread-safe. And I don't feel like spending time testing a logger to try to figure out if it is or not.
For the time being I'm using log4r which is thread-safe, but it's overkill if the standard library already does it.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
快速查看 logger.rb 会发现如下代码:
因此,虽然我无法保证它是否获得线程安全正确,但我可以确认它正在齐心协力地做到这一点正确的!
PS 通过阅读代码,您自己通常很容易回答这样的问题:-)
A quick look at logger.rb reveals code such as the following:
So while I can't vouch for whether it gets thread-safety correct, I can confirm that it is making a concerted effort to do it right!
P.S. It's often easy to answer questions like this for yourself by reading the code :-)
以下是我的原始回复,实际上是错误。请阅读下面 Nemo157 的评论。我把它留在这里仅供参考。
原文:
我认为这并不重要。到目前为止,我所知道的所有 Ruby 实现都一次有效地运行一个线程:它确实允许您启动多个线程,但每个进程一次只能运行一个线程。
Below is my original response, which is actually wrong. Read Nemo157's comment below. I left it here for reference only.
Original:
I don't think it matters. All implementations of Ruby that I know of so far effectively run one thread at a time anyway: It does allow you to start many threads, but only one thread runs at a time per process.
一些 Ruby 类被设计为线程安全的,但在其文档中并没有用一个音节的文字明确说明这一点。与其他编程语言(例如 PHP)中的文档不同。
我记得在 Stack Overflow 上有人问
Queue
是否是线程安全的,即使是,文档也没有说明这一点。Some Ruby classes are designed to be thread safe, but don't explicitly say so in words of one syllable in their documentation. Unlike documentation in other programming languages such as PHP.
I remember being asked whether
Queue
was thread-safe on Stack Overflow, and even though it was, the documentation didn't spell that out.source
尝试日志是否会在多线程中混合
测试日志文件
原因日志没有混合:
source
Try if logs will be mixtures in multithreads
Testing the log file
The reason why logs didn't get mixtured: