EventMachine暂停等待响应
好吧,我有使用 Cramp\Tramp => 的代码引擎盖下的事件机器。代码:
class RetrieveController < ApplicationController
on_start :retrieve_messages
#nonimportant stuff
def receive_messages
#other stuff
@current_user = false
User.where(User[:id].eq("request.env['rack.session']['user_id']")).all method(:retrieve_current_user)
if wait_for_current_user
EM.add_periodic_timer(1) {wait_for_current_user}
else
render @current_user
finish
end
end
def wait_for_current_user
if @current_user
render "current_user is set"
true
else
render "waiting for current_user"
false
end
end
def retrieve_current_user(users)
users.each do |user|
@current_user = user.name
end
end
end
我需要查询结果才能继续在控制器操作中执行,但看起来在我获取数据之前执行已完成。 渲染文本为:
waiting for current_user false
我的 gemfile 是:
source 'http://rubygems.org'
gem 'cramp', '~> 0.12'
gem 'tramp', '~> 0.2'
gem 'activesupport', '3.0.4'
gem 'rack', '~> 1.2.1'
gem 'eventmachine', '~> 0.12.10'
gem 'usher', '~> 0.8.3'
gem 'thin', '~> 1.2.7'
gem "bcrypt-ruby", :require => "bcrypt"
Okay i have code that using Cramp\Tramp => EventMachine under the hood. Code:
class RetrieveController < ApplicationController
on_start :retrieve_messages
#nonimportant stuff
def receive_messages
#other stuff
@current_user = false
User.where(User[:id].eq("request.env['rack.session']['user_id']")).all method(:retrieve_current_user)
if wait_for_current_user
EM.add_periodic_timer(1) {wait_for_current_user}
else
render @current_user
finish
end
end
def wait_for_current_user
if @current_user
render "current_user is set"
true
else
render "waiting for current_user"
false
end
end
def retrieve_current_user(users)
users.each do |user|
@current_user = user.name
end
end
end
I need results from the query to continue execution in controller action, but it looks like execution finished before i get data.
Rendered text is:
waiting for current_user
false
my gemfile is:
source 'http://rubygems.org'
gem 'cramp', '~> 0.12'
gem 'tramp', '~> 0.2'
gem 'activesupport', '3.0.4'
gem 'rack', '~> 1.2.1'
gem 'eventmachine', '~> 0.12.10'
gem 'usher', '~> 0.8.3'
gem 'thin', '~> 1.2.7'
gem "bcrypt-ruby", :require => "bcrypt"
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我没有使用过 Cramp/Tramp,如果我的假设不正确,那么很抱歉,但根据我的理解,这是发生的事情:
问题出在您的方法中的条件中。我很抱歉没有提供代码的固定版本,但希望您现在知道错误在哪里,可以自己找出来。
I haven't used Cramp/Tramp, so sorry if my assumptions are incorrect but here is what is going on from what I can understand:
The problem is in conditional in your method. I'm sorry for not giving the fixed version of your code but hopefully you can figure it out yourself now that you know where the error is.