Skip to content
Browse files

Add a threaded example as well

  • Loading branch information...
1 parent 9a63a58 commit 9f19b958b0cb06ac397ff1311f1d6bffe8d16bb8 @methodmissing methodmissing committed with Aug 7, 2010
Showing with 20 additions and 0 deletions.
  1. +20 −0 examples/threaded.rb
View
20 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

0 comments on commit 9f19b95

Please sign in to comment.
Something went wrong with that request. Please try again.