如何在我的场景中从线程获取返回值?

时间:2022-04-14 00:41:11

So I'm wanting to parse a table on about 10 websites, so I want to create a new thread for each site. However, I'm not exactly sure how to return the data from this type of request.

所以我想在大约10个网站上解析表格,所以我想为每个网站创建一个新的主题。但是,我不确定如何从这种类型的请求中返回数据。

Here's one class:

这是一堂课:

class TestRequest

  def initialize

  end

  def start
    urls = ['site1','site2','site3']
    existing_data = Data.pluck(:symbol, :page)
    data = GetData.pool(size: 10)

    urls.each do |url|
      data.async.perform_requests(url, existing_data)
    end
  end
end

and then GetData class looks like this:

然后GetData类看起来像这样:

require 'celluloid/current'

class GetData
  include Celluloid

  def perform_requests(url, existing_data)
    # perform HTTP request
    # parse HTTP response
    # return returned data ???
  end
end

What I'd ultimately like to do is have an instance variable in TestRequest class and simply add the returned value from GetData into that instance variable from the TestRequest class. After the threads are finished, I want to perform another action using the data in the instance variable.

我最不想做的是在TestRequest类中有一个实例变量,只需将GetData返回的值添加到TestRequest类的实例变量中。线程完成后,我想使用实例变量中的数据执行另一个操作。

I tried playing around with attr_reader, but it doesn't seem to play in my favor.

我尝试使用attr_reader,但它似乎并没有对我有利。

I tried this:

我试过这个:

class TestRequest

  def initialize

  end

  def start
    @returned_data = []
    urls = ['site1','site2','site3']
    existing_data = Data.pluck(:symbol, :page)
    data = GetData.pool(size: 10)

    urls.each do |url|
      data.async.perform_requests(url, existing_data)
    end
  end
  attr_reader :returned_data
end

and then

require 'celluloid/current'

class GetData
  include Celluloid

  def perform_requests(tr, existing_data)
        # perform HTTP request
        # parse HTTP response

        t = TestData.new
        t.returned_data << "value"
  end
end

but this doesn't work either.

但这也不起作用。

1 个解决方案

#1


0  

Multi-threading and Ruby on Rails don't mix very well.

多线程和Ruby on Rails不能很好地混合。

However, you should consider using the ActiveJob documentation (http://guides.rubyonrails.org/active_job_basics.html).

但是,您应该考虑使用ActiveJob文档(http://guides.rubyonrails.org/active_job_basics.html)。

With ActiveJob, you can enqueue jobs and have them executed in the background. There are hooks method defined as well to notify you when a job is about to start, is running or have finished.

使用ActiveJob,您可以将作业排入队列并在后台执行它们。还定义了钩子方法,以便在作业即将开始,正在运行或已完成时通知您。

#1


0  

Multi-threading and Ruby on Rails don't mix very well.

多线程和Ruby on Rails不能很好地混合。

However, you should consider using the ActiveJob documentation (http://guides.rubyonrails.org/active_job_basics.html).

但是,您应该考虑使用ActiveJob文档(http://guides.rubyonrails.org/active_job_basics.html)。

With ActiveJob, you can enqueue jobs and have them executed in the background. There are hooks method defined as well to notify you when a job is about to start, is running or have finished.

使用ActiveJob,您可以将作业排入队列并在后台执行它们。还定义了钩子方法,以便在作业即将开始,正在运行或已完成时通知您。