Skip to content

Instantly share code, notes, and snippets.

@jacobat
Created March 7, 2020 13:43
Show Gist options
  • Save jacobat/324af6456daf2cc32fcb3212c269b1e8 to your computer and use it in GitHub Desktop.
Save jacobat/324af6456daf2cc32fcb3212c269b1e8 to your computer and use it in GitHub Desktop.
Nokogiri blocks Ruby threads
I, [2020-03-07T14:40:36.587947 #90282] INFO -- : *** Threaded ***
I, [2020-03-07T14:40:36.588335 #90282] INFO -- : Still running
I, [2020-03-07T14:40:36.588399 #90282] INFO -- : Start Nokogiri
I, [2020-03-07T14:40:36.588566 #90282] INFO -- : Start Nokogiri
I, [2020-03-07T14:40:36.589041 #90282] INFO -- : Start Nokogiri
I, [2020-03-07T14:40:36.589079 #90282] INFO -- : Start Nokogiri
I, [2020-03-07T14:40:36.589113 #90282] INFO -- : Start Nokogiri
I, [2020-03-07T14:40:36.589138 #90282] INFO -- : Start Nokogiri
I, [2020-03-07T14:40:36.589157 #90282] INFO -- : Start Nokogiri
I, [2020-03-07T14:40:36.589175 #90282] INFO -- : Start Nokogiri
I, [2020-03-07T14:40:36.589203 #90282] INFO -- : Start Nokogiri
I, [2020-03-07T14:40:53.202677 #90282] INFO -- : Done with Nokogiri
I, [2020-03-07T14:40:53.202883 #90282] INFO -- : 15.884452 0.731071 16.615523 ( 16.614189)
I, [2020-03-07T14:40:36.589230 #90282] INFO -- : Start Nokogiri
I, [2020-03-07T14:40:58.180972 #90282] INFO -- : Done with Nokogiri
I, [2020-03-07T14:40:55.004381 #90282] INFO -- : Done with Nokogiri
I, [2020-03-07T14:41:04.129960 #90282] INFO -- : 26.442706 1.077119 27.519825 ( 27.540618)
I, [2020-03-07T14:40:39.136656 #90282] INFO -- : Still running
I, [2020-03-07T14:41:01.510371 #90282] INFO -- : Done with Nokogiri
I, [2020-03-07T14:41:09.359079 #90282] INFO -- : 31.558369 1.183865 32.742234 ( 32.770588)
I, [2020-03-07T14:41:01.471189 #90282] INFO -- : 20.684099 0.879757 21.563856 ( 24.860487)
I, [2020-03-07T14:41:07.811329 #90282] INFO -- : Done with Nokogiri
I, [2020-03-07T14:41:12.403081 #90282] INFO -- : 29.409876 0.920205 30.330081 ( 30.409325)
I, [2020-03-07T14:41:12.425036 #90282] INFO -- : Done with Nokogiri
I, [2020-03-07T14:41:12.425155 #90282] INFO -- : 19.399526 0.596986 19.996512 ( 20.026801)
I, [2020-03-07T14:41:09.358970 #90282] INFO -- : Done with Nokogiri
I, [2020-03-07T14:41:09.359113 #90282] INFO -- : Still running
I, [2020-03-07T14:41:04.129855 #90282] INFO -- : Done with Nokogiri
I, [2020-03-07T14:41:12.425377 #90282] INFO -- : 34.518372 1.281099 35.799471 ( 35.835193)
I, [2020-03-07T14:41:12.279499 #90282] INFO -- : Done with Nokogiri
I, [2020-03-07T14:41:12.425487 #90282] INFO -- : 34.490696 1.224212 35.714908 ( 35.778546)
I, [2020-03-07T14:41:12.402922 #90282] INFO -- : Done with Nokogiri
I, [2020-03-07T14:41:12.425627 #90282] INFO -- : 25.617177 0.803064 26.420241 ( 26.481769)
I, [2020-03-07T14:41:12.425700 #90282] INFO -- : 16.899154 0.489671 17.388825 ( 17.421087)
I, [2020-03-07T14:41:12.425754 #90282] INFO -- : *** Forked ***
I, [2020-03-07T14:41:12.437300 #90311] INFO -- : Start Nokogiri
I, [2020-03-07T14:41:12.437680 #90312] INFO -- : Start Nokogiri
I, [2020-03-07T14:41:12.438253 #90313] INFO -- : Start Nokogiri
I, [2020-03-07T14:41:12.438845 #90314] INFO -- : Start Nokogiri
I, [2020-03-07T14:41:12.439781 #90315] INFO -- : Start Nokogiri
I, [2020-03-07T14:41:12.440355 #90316] INFO -- : Start Nokogiri
I, [2020-03-07T14:41:12.441231 #90317] INFO -- : Start Nokogiri
I, [2020-03-07T14:41:12.442495 #90318] INFO -- : Start Nokogiri
I, [2020-03-07T14:41:12.443786 #90319] INFO -- : Start Nokogiri
I, [2020-03-07T14:41:12.445278 #90320] INFO -- : Start Nokogiri
I, [2020-03-07T14:41:13.429324 #90282] INFO -- : Still running
I, [2020-03-07T14:41:14.430136 #90282] INFO -- : Still running
I, [2020-03-07T14:41:15.430589 #90282] INFO -- : Still running
I, [2020-03-07T14:41:16.431122 #90282] INFO -- : Still running
I, [2020-03-07T14:41:17.433490 #90282] INFO -- : Still running
I, [2020-03-07T14:41:18.445574 #90282] INFO -- : Still running
I, [2020-03-07T14:41:19.449898 #90282] INFO -- : Still running
I, [2020-03-07T14:41:20.454168 #90282] INFO -- : Still running
I, [2020-03-07T14:41:21.457702 #90282] INFO -- : Still running
I, [2020-03-07T14:41:22.457894 #90282] INFO -- : Still running
I, [2020-03-07T14:41:23.459269 #90282] INFO -- : Still running
I, [2020-03-07T14:41:24.462634 #90282] INFO -- : Still running
I, [2020-03-07T14:41:25.136144 #90312] INFO -- : Done with Nokogiri
I, [2020-03-07T14:41:25.140289 #90312] INFO -- : 7.455782 1.017214 8.472996 ( 12.701245)
I, [2020-03-07T14:41:25.152517 #90313] INFO -- : Done with Nokogiri
I, [2020-03-07T14:41:25.153942 #90313] INFO -- : 7.452782 1.016807 8.469589 ( 12.714258)
I, [2020-03-07T14:41:25.164082 #90318] INFO -- : Done with Nokogiri
I, [2020-03-07T14:41:25.168981 #90316] INFO -- : Done with Nokogiri
I, [2020-03-07T14:41:25.171387 #90318] INFO -- : 7.449637 1.019224 8.468861 ( 12.726622)
I, [2020-03-07T14:41:25.173028 #90316] INFO -- : 7.429858 0.999727 8.429585 ( 12.731311)
I, [2020-03-07T14:41:25.181784 #90314] INFO -- : Done with Nokogiri
I, [2020-03-07T14:41:25.183468 #90314] INFO -- : 7.462105 1.025580 8.487685 ( 12.743272)
I, [2020-03-07T14:41:25.194311 #90317] INFO -- : Done with Nokogiri
I, [2020-03-07T14:41:25.195826 #90317] INFO -- : 7.456276 1.015231 8.471507 ( 12.753052)
I, [2020-03-07T14:41:25.205521 #90311] INFO -- : Done with Nokogiri
I, [2020-03-07T14:41:25.207145 #90311] INFO -- : 7.465108 1.040545 8.505653 ( 12.768246)
I, [2020-03-07T14:41:25.225940 #90315] INFO -- : Done with Nokogiri
I, [2020-03-07T14:41:25.227715 #90315] INFO -- : 7.440923 1.011682 8.452605 ( 12.786527)
I, [2020-03-07T14:41:25.323806 #90319] INFO -- : Done with Nokogiri
I, [2020-03-07T14:41:25.329500 #90319] INFO -- : 7.457472 0.966802 8.424274 ( 12.882557)
I, [2020-03-07T14:41:25.356879 #90320] INFO -- : Done with Nokogiri
I, [2020-03-07T14:41:25.358468 #90320] INFO -- : 7.431287 0.962229 8.393516 ( 12.910682)
I, [2020-03-07T14:41:25.466682 #90282] INFO -- : Still running
I, [2020-03-07T14:41:26.467385 #90282] INFO -- : Done
require 'pathname'
require 'nokogiri'
require 'benchmark'
require 'logger'
def format_xslt
file = File.open(Pathname.new("app").join("xslt", "xml-json", "isbn.xsl"), "rb")
format_xslt = Nokogiri::XSLT(
file
)
file.close
format_xslt
end
def run
$logger.info "Start Nokogiri"
bench = Benchmark.measure do
format_xslt.transform(xml_document).to_xml.tap { $logger.info "Done with Nokogiri" }
end
$logger.info bench.to_s.strip
end
def xml_document
Nokogiri::XML(File.read(ARGV[0]))
end
$logger = Logger.new(STDOUT)
$logger.info "*** Threaded ***"
$running = true
t = Thread.new do
while $running
$logger.info "Still running"
sleep 1
end
end
threads = 10.times.map do
Thread.new { run }
end
threads.each(&:join)
$logger.info "*** Forked ***"
10.times.map do
fork { run }
end
Process.waitall
$running = false
t.join
$logger.info "Done"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment