English 中文(简体)
红宝石 - 无法创建线索 (35) (线索错误)
原标题:ruby - can t create Thread (35) (ThreadError)

我对红宝石非常新, 正在学习如何用多个线条进行处理。 我所做的是使用 Nokogiri 分析一个 170mbxml 文件, 并且将数据库( Postgressql) 插入到我的. each () 中的新线内。 请建议一个更好的方法来处理这个非常大的文件, 并且用多个线条来做。 这里是我目前拥有的 。

    conn = PGconn.connect("localhost", 5432, "", "", "oaxis","postgres","root")

    f = File.open("metadata.xml")
    doc = Nokogiri::XML(f)

    counter = 0

    threadArray = []

    doc.xpath( //Title ).each do |node|
        threadArray[counter] = Thread.new{
        titleVal = node.text
        random_string = (0...10).map{ ( a .. z ).to_a[rand(26)] }.join

        conn.prepare( ins +random_string,  insert into sample_tbl (title) values ($1) )
        conn.exec_prepared( ins +random_string, [titleVal])

        puts titleVal+" ==>"+random_string+ " 
"

        counter += 1
       }

    end

threadArray.each {|t| t.join}

f.close
问题回答

您正在做的事情不会导致数据被更快地插入数据库, 与单一已读案例相比。 MRI Ruby 拥有一个全局翻译锁, 并且一次只运行一条线。 使用 MRI Ruby 中的线索只会在线索执行 IO 动作( 或等待能够这样做) 时提高性能, 而程序进度并不取决于这些 IO 动作的结果( 所以您不积极等待) 。

我建议您不要在此使用线索, 而是计算您想要插入的所有值, 并将其质量插入 。 代码也会更简单, 更便于理解和理解 。 即使从一个线条中逐个插入, 也会更快, 但没有理由这样做 。





相关问题
Ruby parser in Java

The project I m doing is written in Java and parsers source code files. (Java src up to now). Now I d like to enable parsing Ruby code as well. Therefore I am looking for a parser in Java that parses ...

rails collection_select vs. select

collection_select and select Rails helpers: Which one should I use? I can t see a difference in both ways. Both helpers take a collection and generates options tags inside a select tag. Is there a ...

RubyCAS-Client question: Rails

I ve installed RubyCAS-Client version 2.1.0 as a plugin within a rails app. It s working, but I d like to remove the ?ticket= in the url. Is this possible?

Ordering a hash to xml: Rails

I m building an xml document from a hash. The xml attributes need to be in order. How can this be accomplished? hash.to_xml

multiple ruby extension modules under one directory

Can sources for discrete ruby extension modules live in the same directory, controlled by the same extconf.rb script? Background: I ve a project with two extension modules, foo.so and bar.so which ...

Text Editor for Ruby-on-Rails

guys which text editor is good for Rubyonrails? i m using Windows and i was using E-Texteditor but its not free n its expired now can anyone plese tell me any free texteditor? n which one is best an ...

热门标签