EventMachine暂停等待响应

发布于 2024-10-26 13:40:09 字数 1164 浏览 2 评论 0原文

好吧,我有使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

夏花。依旧 2024-11-02 13:40:09

我没有使用过 Cramp/Tramp,如果我的假设不正确,那么很抱歉,但根据我的理解,这是发生的事情:

def received_messages
  # other stuff
  # we set @current_user to false <-- this is important
  @current_user = false
  # we are using tramp (async orm) this is not going to block the execution
  # of this method, so we will start executing this but we will also proceed
  # to the next step in this method (if statement after this)
  User.where(User[:id].eq("request.env['rack.session']['user_id']")).all method(:retrieve_current_user)
  # So the query is starting its execution but we are already at this if
  # statement and we execute the "wait_for_current_user" method and at this
  # point of time @current_user is still set to false <--- because of that
  # your wait_for_current_user method is printing "waiting for current user"
  # AND it returns false to this if statement <--- because of that we DO NOT
  # add the periodic timer (maybe change if to unless)
  if wait_for_current_user
    # we do not enter here because the wait_for_current_user returned false
    EM.add_periodic_timer(1) {wait_for_current_user}
  else
    # we go here and we execute "render false" which simply prints "false"
    render @current_user
    # at this point the query started on the User model might still be
    # running but here we go and execute the finish command and all you
    # get is "waiting for current_user false"
    finish
  end

问题出在您的方法中的条件中。我很抱歉没有提供代码的固定版本,但希望您现在知道错误在哪里,可以自己找出来。

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:

def received_messages
  # other stuff
  # we set @current_user to false <-- this is important
  @current_user = false
  # we are using tramp (async orm) this is not going to block the execution
  # of this method, so we will start executing this but we will also proceed
  # to the next step in this method (if statement after this)
  User.where(User[:id].eq("request.env['rack.session']['user_id']")).all method(:retrieve_current_user)
  # So the query is starting its execution but we are already at this if
  # statement and we execute the "wait_for_current_user" method and at this
  # point of time @current_user is still set to false <--- because of that
  # your wait_for_current_user method is printing "waiting for current user"
  # AND it returns false to this if statement <--- because of that we DO NOT
  # add the periodic timer (maybe change if to unless)
  if wait_for_current_user
    # we do not enter here because the wait_for_current_user returned false
    EM.add_periodic_timer(1) {wait_for_current_user}
  else
    # we go here and we execute "render false" which simply prints "false"
    render @current_user
    # at this point the query started on the User model might still be
    # running but here we go and execute the finish command and all you
    # get is "waiting for current_user false"
    finish
  end

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文