Skip to content

Instantly share code, notes, and snippets.

@aselder
Created May 22, 2012 01:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save aselder/2766012 to your computer and use it in GitHub Desktop.
Save aselder/2766012 to your computer and use it in GitHub Desktop.
Test script demonstrating Nokogiri thread safety
require 'open-uri'
require 'nokogiri'
MS_ARRAY_SCHEMA = "http://schemas.microsoft.com/2003/10/Serialization/Arrays".freeze
def job
begin
data = Nokogiri::XML(open("file.xml"))
rows = data.xpath(".//xmlns:ArrayOfTicket/xmlns:Ticket")
rows.each do |row|
row.xpath(".//xmlns:Id").first
row.xpath(".//xmlns:DisplayPrice").first
row.xpath(".//xmlns:AvailableBlocks").first.xpath("a:int", {"a" => MS_ARRAY_SCHEMA}).collect{|elem| elem.text.to_i}
end
puts "OK"
rescue Nokogiri::XML::XPath::SyntaxError => ex
puts "XPath Syntax Error: #{ex}"
end
end
threads = (0...100).to_a.map {Thread.new {job}}
threads.each {|t| t.join}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment