Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class DeferredBookLoader
- include EM::Deferrable
- def load_books
- pg = PG::EM::Client.new(:user => "user",:password => "password",:dbname => 'library_development')
- q = pg.query("select * from books")
- q.callback do |res|
- long_op = proc do
- res.map {|e| e["name"]}
- end
- finished = proc {|results| succeed(results)}
- EM.defer(long_op,finished)
- end
- q.errback do |ex|
- fail(ex)
- end
- end
- end
- EM.run do
- $stdout.sync = true
- last_tick = Time.now
- EM.add_periodic_timer(0.5) do
- current = Time.now
- puts "Last tick:#{last_tick} now: #{current}"
- last_tick = current
- end
- EM.add_periodic_timer(13) do deferred = DeferredBookLoader.new
- deferred.load_books
- deferred.callback do |results|
- puts "Loaded #{results.size}"
- puts results[3]
- end
- deferred.errback do |results|
- puts "Failed #{results}"
- end
- end
- end
- Last tick:2012-09-30 21:37:19 +0300 now: 2012-09-30 21:37:20 +0300
- Last tick:2012-09-30 21:37:20 +0300 now: 2012-09-30 21:37:20 +0300
- Last tick:2012-09-30 21:37:20 +0300 now: 2012-09-30 21:37:21 +0300
- Last tick:2012-09-30 21:37:21 +0300 now: 2012-09-30 21:37:21 +0300
- Last tick:2012-09-30 21:37:21 +0300 now: 2012-09-30 21:37:22 +0300
- Last tick:2012-09-30 21:37:22 +0300 now: 2012-09-30 21:37:22 +0300
- Last tick:2012-09-30 21:37:22 +0300 now: 2012-09-30 21:37:23 +0300
- Last tick:2012-09-30 21:37:23 +0300 now: 2012-09-30 21:37:23 +0300
- Last tick:2012-09-30 21:37:23 +0300 now: 2012-09-30 21:37:24 +0300
- Last tick:2012-09-30 21:37:24 +0300 now: 2012-09-30 21:37:24 +0300
- Last tick:2012-09-30 21:37:24 +0300 now: 2012-09-30 21:37:25 +0300
- Last tick:2012-09-30 21:37:25 +0300 now: 2012-09-30 21:37:25 +0300
- Last tick:2012-09-30 21:37:25 +0300 now: 2012-09-30 21:37:26 +0300
- Loaded 200000
- Book 133681
- Last tick:2012-09-30 21:37:26 +0300 now: 2012-09-30 21:37:27 +0300
- Last tick:2012-09-30 21:37:27 +0300 now: 2012-09-30 21:37:28 +0300
- Last tick:2012-09-30 21:37:28 +0300 now: 2012-09-30 21:37:28 +0300
- Last tick:2012-09-30 21:37:28 +0300 now: 2012-09-30 21:37:29 +0300
- Last tick:2012-09-30 21:37:29 +0300 now: 2012-09-30 21:37:29 +0300
- Last tick:2012-09-30 21:37:29 +0300 now: 2012-09-30 21:37:30 +0300
- Last tick:2012-09-30 21:37:30 +0300 now: 2012-09-30 21:37:30 +0300
- Last tick:2012-09-30 21:37:30 +0300 now: 2012-09-30 21:37:31 +0300
- Last tick:2012-09-30 21:37:31 +0300 now: 2012-09-30 21:37:31 +0300
- Last tick:2012-09-30 21:37:31 +0300 now: 2012-09-30 21:37:32 +0300
- Last tick:2012-09-30 21:37:32 +0300 now: 2012-09-30 21:37:32 +0300
- Last tick:2012-09-30 21:37:32 +0300 now: 2012-09-30 21:37:33 +0300
- Last tick:2012-09-30 21:37:33 +0300 now: 2012-09-30 21:37:33 +0300
- Last tick:2012-09-30 21:37:33 +0300 now: 2012-09-30 21:37:34 +0300
- Last tick:2012-09-30 21:37:34 +0300 now: 2012-09-30 21:37:34 +0300
- Last tick:2012-09-30 21:37:34 +0300 now: 2012-09-30 21:37:35 +0300
- Last tick:2012-09-30 21:37:35 +0300 now: 2012-09-30 21:37:35 +0300
- Last tick:2012-09-30 21:37:35 +0300 now: 2012-09-30 21:37:36 +0300
- Last tick:2012-09-30 21:37:36 +0300 now: 2012-09-30 21:37:36 +0300
- Last tick:2012-09-30 21:37:36 +0300 now: 2012-09-30 21:37:37 +0300
- Last tick:2012-09-30 21:37:37 +0300 now: 2012-09-30 21:37:37 +0300
- Last tick:2012-09-30 21:37:37 +0300 now: 2012-09-30 21:37:38 +0300
- Last tick:2012-09-30 21:37:38 +0300 now: 2012-09-30 21:37:38 +0300
- Last tick:2012-09-30 21:37:38 +0300 now: 2012-09-30 21:37:39 +0300
- Loaded 200000
- Book 133681
- Last tick:2012-09-30 21:37:39 +0300 now: 2012-09-30 21:37:40 +0300
Add Comment
Please, Sign In to add comment