Ruby 的 stdlib Logger 类是线程安全的吗?

发布于 2024-10-11 04:18:16 字数 278 浏览 6 评论 0原文

简而言之,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 技术交流群。

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

发布评论

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

评论(4

爱要勇敢去追 2024-10-18 04:18:16

快速查看 logger.rb 会发现如下代码:

def write(message)
  @mutex.synchronize do
    if @shift_age and @dev.respond_to?(:stat)
      begin
        check_shift_log
      rescue
        raise Logger::ShiftingError.new("Shifting failed. #{$!}")
      end
    end
    @dev.write(message)
  end
end

因此,虽然我无法保证它是否获得线程安全正确,但我可以确认它正在齐心协力地做到这一点正确的!

PS 通过阅读代码,您自己通常很容易回答这样的问题:-)

A quick look at logger.rb reveals code such as the following:

def write(message)
  @mutex.synchronize do
    if @shift_age and @dev.respond_to?(:stat)
      begin
        check_shift_log
      rescue
        raise Logger::ShiftingError.new("Shifting failed. #{$!}")
      end
    end
    @dev.write(message)
  end
end

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 :-)

抠脚大汉 2024-10-18 04:18:16

以下是我的原始回复,实际上是错误。请阅读下面 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.

岁月静好 2024-10-18 04:18:16

一些 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.

仄言 2024-10-18 04:18:16

source

尝试日志是否会在多线程中混合

require 'logger'
require 'parallel'

logger = Logger.new("/tmp/test.log")

Parallel.map(['a','b'], :in_threads => 2) do |letter|
  1000.times do
    logger.info letter * 5000
  end
end

测试日志文件

egrep -e 'ab' -e 'ba' /tmp/test.log
[empty]

原因日志没有混合:

def write(message)
  @mutex.synchronize do
    ...    
    @dev.write(message)        
  end
end

source

Try if logs will be mixtures in multithreads

require 'logger'
require 'parallel'

logger = Logger.new("/tmp/test.log")

Parallel.map(['a','b'], :in_threads => 2) do |letter|
  1000.times do
    logger.info letter * 5000
  end
end

Testing the log file

egrep -e 'ab' -e 'ba' /tmp/test.log
[empty]

The reason why logs didn't get mixtured:

def write(message)
  @mutex.synchronize do
    ...    
    @dev.write(message)        
  end
end
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文