首页 > 解决方案 > 使用 htop 命令可视化 ruby​​ 线程

问题描述

我正在试验 ruby​​ 中的多线程。我运行了这段同时运行 3 个线程的代码(ruby threads.rb在我的终端中):

arr = []
arr.push(Thread.new do
  1000000.times do |i|
    puts "thread 1"
  end
end)
arr.push(Thread.new do
  1000000.times do |i|
    puts "thread 2"
  end
end)

arr.push(Thread.new do
  1000000.times do |i|
    puts "thread 3"
  end
end)

arr.each {|t| t.join}

我现在htop在终端的树视图中运行,看看我是否真的可以看到 3 个不同的线程:

在此处输入图像描述

我认为线程threads.rb 是突出显示的线程正下方的行,但我看不到我的三个启动线程作为threads.rb 进程的分支。红宝石线程与使用htop 显示的线程和进程无关吗?有没有办法可视化在我的 threads.rb 进程中运行的不同 ruby​​ 线程。

标签: rubymultithreadinghtop

解决方案


这取决于您的 Ruby 解释器。MRI 应该为每个 Ruby 线程分配一个本地线程。我已经运行了您的脚本,并且可以看到htop其中的线程(线程超过 3 个,但这一定是解释器所做的):

htop

尝试设置过滤器htop(点击F4并键入ruby过滤器字符串)。

编辑:我已经在 Debian 上进行了测试。htop在 MacOS 上显然不显示线程。


推荐阅读