Guest User

Untitled

a guest
Jan 17th, 2019
124
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.46 KB | None | 0 0
  1. class DeferredBookLoader
  2. include EM::Deferrable
  3. def load_books
  4. pg = PG::EM::Client.new(:user => "user",:password => "password",:dbname => 'library_development')
  5.  
  6. q = pg.query("select * from books")
  7.  
  8. q.callback do |res|
  9. long_op = proc do
  10. res.map {|e| e["name"]}
  11. end
  12.  
  13. finished = proc {|results| succeed(results)}
  14. EM.defer(long_op,finished)
  15. end
  16.  
  17. q.errback do |ex|
  18. fail(ex)
  19. end
  20.  
  21. end
  22. end
  23.  
  24. EM.run do
  25. $stdout.sync = true
  26.  
  27. last_tick = Time.now
  28. EM.add_periodic_timer(0.5) do
  29. current = Time.now
  30. puts "Last tick:#{last_tick} now: #{current}"
  31. last_tick = current
  32. end
  33.  
  34. EM.add_periodic_timer(13) do deferred = DeferredBookLoader.new
  35. deferred.load_books
  36.  
  37. deferred.callback do |results|
  38. puts "Loaded #{results.size}"
  39. puts results[3]
  40. end
  41.  
  42. deferred.errback do |results|
  43. puts "Failed #{results}"
  44. end
  45. end
  46.  
  47. end
  48.  
  49. Last tick:2012-09-30 21:37:19 +0300 now: 2012-09-30 21:37:20 +0300
  50. Last tick:2012-09-30 21:37:20 +0300 now: 2012-09-30 21:37:20 +0300
  51. Last tick:2012-09-30 21:37:20 +0300 now: 2012-09-30 21:37:21 +0300
  52. Last tick:2012-09-30 21:37:21 +0300 now: 2012-09-30 21:37:21 +0300
  53. Last tick:2012-09-30 21:37:21 +0300 now: 2012-09-30 21:37:22 +0300
  54. Last tick:2012-09-30 21:37:22 +0300 now: 2012-09-30 21:37:22 +0300
  55. Last tick:2012-09-30 21:37:22 +0300 now: 2012-09-30 21:37:23 +0300
  56. Last tick:2012-09-30 21:37:23 +0300 now: 2012-09-30 21:37:23 +0300
  57. Last tick:2012-09-30 21:37:23 +0300 now: 2012-09-30 21:37:24 +0300
  58. Last tick:2012-09-30 21:37:24 +0300 now: 2012-09-30 21:37:24 +0300
  59. Last tick:2012-09-30 21:37:24 +0300 now: 2012-09-30 21:37:25 +0300
  60. Last tick:2012-09-30 21:37:25 +0300 now: 2012-09-30 21:37:25 +0300
  61. Last tick:2012-09-30 21:37:25 +0300 now: 2012-09-30 21:37:26 +0300
  62. Loaded 200000
  63. Book 133681
  64. Last tick:2012-09-30 21:37:26 +0300 now: 2012-09-30 21:37:27 +0300
  65. Last tick:2012-09-30 21:37:27 +0300 now: 2012-09-30 21:37:28 +0300
  66. Last tick:2012-09-30 21:37:28 +0300 now: 2012-09-30 21:37:28 +0300
  67. Last tick:2012-09-30 21:37:28 +0300 now: 2012-09-30 21:37:29 +0300
  68. Last tick:2012-09-30 21:37:29 +0300 now: 2012-09-30 21:37:29 +0300
  69. Last tick:2012-09-30 21:37:29 +0300 now: 2012-09-30 21:37:30 +0300
  70. Last tick:2012-09-30 21:37:30 +0300 now: 2012-09-30 21:37:30 +0300
  71. Last tick:2012-09-30 21:37:30 +0300 now: 2012-09-30 21:37:31 +0300
  72. Last tick:2012-09-30 21:37:31 +0300 now: 2012-09-30 21:37:31 +0300
  73. Last tick:2012-09-30 21:37:31 +0300 now: 2012-09-30 21:37:32 +0300
  74. Last tick:2012-09-30 21:37:32 +0300 now: 2012-09-30 21:37:32 +0300
  75. Last tick:2012-09-30 21:37:32 +0300 now: 2012-09-30 21:37:33 +0300
  76. Last tick:2012-09-30 21:37:33 +0300 now: 2012-09-30 21:37:33 +0300
  77. Last tick:2012-09-30 21:37:33 +0300 now: 2012-09-30 21:37:34 +0300
  78. Last tick:2012-09-30 21:37:34 +0300 now: 2012-09-30 21:37:34 +0300
  79. Last tick:2012-09-30 21:37:34 +0300 now: 2012-09-30 21:37:35 +0300
  80. Last tick:2012-09-30 21:37:35 +0300 now: 2012-09-30 21:37:35 +0300
  81. Last tick:2012-09-30 21:37:35 +0300 now: 2012-09-30 21:37:36 +0300
  82. Last tick:2012-09-30 21:37:36 +0300 now: 2012-09-30 21:37:36 +0300
  83. Last tick:2012-09-30 21:37:36 +0300 now: 2012-09-30 21:37:37 +0300
  84. Last tick:2012-09-30 21:37:37 +0300 now: 2012-09-30 21:37:37 +0300
  85. Last tick:2012-09-30 21:37:37 +0300 now: 2012-09-30 21:37:38 +0300
  86. Last tick:2012-09-30 21:37:38 +0300 now: 2012-09-30 21:37:38 +0300
  87. Last tick:2012-09-30 21:37:38 +0300 now: 2012-09-30 21:37:39 +0300
  88. Loaded 200000
  89. Book 133681
  90. 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