examples/async.rb in swift-0.4.3 vs examples/async.rb in swift-0.5.0

- old
+ new

@@ -17,39 +17,44 @@ sample = DATA.read.split(/\n/).map {|v| v.split(/\t+/) } puts '-- insert --' ins = db.prepare('insert into users(name, email) values(?, ?)') - 10.times {|n| ins.execute(*sample[n%3]) } + 9.times {|n| ins.execute(*sample[n%3]) } end +sleep_clause = { + Swift::DB::Postgres => "case length(pg_sleep(%s)::text) when 0 then '%s' else '%s' end as sleep", + Swift::DB::Mysql => "if (sleep(%s), '%s', '%s') as sleep" +} + puts '-- select 9 times with a pool of size 5 --' Swift.trace false Swift.pool(5) do |db| (1..9).each do |n| pause = '%0.3f' % ((20-n)/20.0) - pause = "case length(pg_sleep(#{pause})::text) when 0 then '#{pause}' else '' end as sleep" - db.execute("select #{pause}, * from users where id = ?", n) {|r| p r.first } + pause = sleep_clause[adapter] % 3.times.map { pause } + db.execute("select *, #{pause} from users where id = ?", n) {|r| p r.first } end end Swift.trace true puts '-- multiple pools: size 2, size 1 --' EM.run { pool1 = Swift.pool(2) pool2 = Swift.pool(1) - pool1.execute("select * from users limit 5 offset 0") do |rs| + pool1.execute("select * from users limit 3 offset 0") do |rs| puts '-- Inside pool1 #callback --' rs.each {|r| p r } - pool1.execute("select * from users limit 5 offset 5") do |rs| + pool1.execute("select * from users limit 3 offset 3") do |rs| puts '-- Inside pool1 #callback again --' rs.each {|r| p r } EM.stop end end - pool2.execute("select * from users limit 5 offset 10") do |rs| + pool2.execute("select * from users limit 3 offset 6") do |rs| puts '-- Inside pool2 #callback --' rs.each {|r| p r } end }