Ruby线程在升级到Rails 5后不起作用

问题描述 投票:1回答:1

我有一个使用Service的API,其中我使用Ruby线程来减少API的响应时间。我尝试使用以下示例共享上下文。在Rails 4上运行正常,ruby 2.2.1]

现在,我们已将Rails升级到5.2.3和ruby 2.6.5。之后,服务将停止工作。我可以从Console调用该服务,它工作正常。但是通过API调用,服务在到达CurrencyConverter.new后将变得无响应。任何想法可能是什么问题?


class ParallelTest
  def initialize
    puts "Initialized"
  end

  def perform
    # Our sample set of currencies
    currencies = ['ARS','AUD','CAD','CNY','DEM','EUR','GBP','HKD','ILS','INR','USD','XAG','XAU']
# Create an array to keep track of threads
    threads = []

    currencies.each do |currency|
      # Keep track of the child processes as you spawn them
      threads << Thread.new do
        puts currency
        CurrencyConverter.new(currency).print
      end
    end
# Join on the child processes to allow them to finish
    threads.each do |thread|
      thread.join
    end
    { success: true }
  end
end


class CurrencyConverter
  def initialize(params)
    @curr = params
  end

  def print

    puts @curr
  end
end

如果我删除CurrencyConverter.new(currency),则一切正常。 CurrencyConverter是我拥有的服务对象。

发现问题感谢@anothermh提供此链接https://guides.rubyonrails.org/threading_and_code_execution.html#wrapping-application-codehttps://guides.rubyonrails.org/threading_and_code_execution.html#load-interlock

根据博客,当一个线程通过评估相应文件中的类定义来执行自动加载时,重要的是没有其他线程遇到对部分定义的常量的引用。

一次只能加载或卸载一个线程,并且必须同时等待,直到没有其他线程在运行应用程序代码。如果一个线程正在等待执行加载,它不会阻止其他线程加载(实际上,它们将相互配合,并且在所有继续一起运行之前,每个线程依次执行其排队的加载)。

这可以通过允许并发加载来解决。https://guides.rubyonrails.org/threading_and_code_execution.html#permit-concurrent-loads

Rails.application.executor.wrap do
  urls.each do |currency|
    threads << Thread.new do
        CurrencyConverter.new(currency)
        puts currency
    end
    ActiveSupport::Dependencies.interlock.permit_concurrent_loads do
      threads.map(&:join)
    end
  end
end

谢谢大家的时间,谢谢。

ruby-on-rails ruby ruby-thread
1个回答
0
投票

不要重新发明轮子,而要使用Sidekiq。 😉

从项目页面:

简单,高效的Ruby后台处理。

Sidekiq使用线程在同一进程中同时处理许多作业。它不需要Rails,但将与Rails紧密集成,使后台处理变得简单。

[有400多个贡献者,并且在Github上启动了10k +,他们已经建立了一个可靠的并行作业执行过程,该过程可以进行生产,并且易于设置。

看看他们的Getting Started自己看。

© www.soinside.com 2019 - 2024. All rights reserved.