From 9f19b958b0cb06ac397ff1311f1d6bffe8d16bb8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lourens=20Naud=C3=A9?= Date: Sat, 7 Aug 2010 21:35:00 +0100 Subject: [PATCH] Add a threaded example as well --- examples/threaded.rb | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) create mode 100644 examples/threaded.rb diff --git a/examples/threaded.rb b/examples/threaded.rb new file mode 100644 index 0000000..7d3b961 --- /dev/null +++ b/examples/threaded.rb @@ -0,0 +1,20 @@ +# encoding: utf-8 + +$LOAD_PATH.unshift 'lib' +require 'mysql2' +require 'timeout' + +threads = [] +# Should never exceed worst case 3.5 secs across all 20 threads +Timeout.timeout(3.5) do + 20.times do + threads << Thread.new do + overhead = rand(3) + puts ">> thread #{Thread.current.object_id} query, #{overhead} sec overhead" + # 3 second overhead per query + Mysql2::Client.new(:host => "localhost", :username => "root").query("SELECT sleep(#{overhead}) as result") + puts "<< thread #{Thread.current.object_id} result, #{overhead} sec overhead" + end + end + threads.each{|t| t.join } +end \ No newline at end of file