ruby - 许多实例 - 并行处理

时间:2014-10-02 18:11:08

标签: ruby parallel-processing processing

我是ruby的新手,我不知道为什么我的程序没有按预期工作。 问题是我正在阅读某些网站,我正在尝试阅读一些信息。问题是我不知道如何创建XsidVehicle类的许多实例。 我正在尝试读取200个对象,如果每个实例等到上一个完成,那么读取信息至少需要200秒。我想创建许多具有不同ID的实例,这些实例可以同时读取不同的页面。

require 'open-uri'

class XsidVehicle

  def VehicleID(id)
    @id  = id
    individual_source = open("https://www.motocykle.com/cars/id/#{id}")
    puts individual_source[individual_source.index(id),individual_source.index(id)+12]
  end

end

source = open('https://www.motocykle.pl/cars').read
newItem = source.index('lId=')
endOfThePage = source.index('</html>')
myNewSource = source[newItem.to_i,endOfThePage.to_i]

    for i in 0..199 do
      #puts "This is i: #{i}"

        iid = myNewSource[myNewSource.index('"')+1,20]
        iid = iid[0,iid.index('"').to_i]

        myNewSource = myNewSource[myNewSource.index('class=').to_i,myNewSource.length]
        myNewSource = myNewSource[myNewSource.index('listingId=').to_i,myNewSource.length]

        puts "iid #{i}: #{iid}"     #here i noticed that the class instance is created then it navigates to the website to read it, and then a new instance is created. But I want many instances to be created at the same time, so not to be restricted by ordered processing.
        vehicle = XsidVehicle.new
        vehicle.VehicleID(iid)


    end

0 个答案:

没有答案