【问题标题】:EventMachine and loopingEventMachine 和循环
【发布时间】:2012-08-15 09:19:45
【问题描述】:

这是我的代码:

EventMachine.run {

    conn = EM::Protocols::HttpClient2.connect request.host, 80

    req = conn.get(request.query)
    req.callback { |response|
      p(response.status)
      p(response.headers)
      p(response.content)
    }
}

回调触发,即我得到状态等的字符串输出。

但我想要它做的是触发回调,然后重复。我计划实现更多逻辑,例如每次调整 URL,但现在,我只希望它:

  1. 检索网址
  2. 触发回调
  3. 重复...

我对这种模式的理解是,该循环中的所有内容都会触发,然后返回,然后一直持续下去,直到我执行 EM.stop

现在,它检索 URL 数据,但似乎挂起。

我是否需要进行某种形式的退货才能在此处继续?为什么它会挂起,而不是一遍又一遍地循环?

如果我用循环围绕整个上述代码块 do ... end 它会按预期工作..这是实现这一点的正确方法吗?我想我很困惑,因为我认为 EM.run 中的所有内容在完成时都会重复。

【问题讨论】:

    标签: ruby eventmachine


    【解决方案1】:

    您提供的run 块只运行一次。事件循环不会直接暴露给您,而是旨在不可见的东西。不要将run 块与while 循环混淆。它只运行一次,但在事件循环执行时运行。

    如果你想重复一个操作,你需要创建某种堆栈并完成它,每个回调检查堆栈是否还有更多工作要做,然后发出另一个调用。 EventMachine 应用程序是使用这种回调链接方法构建的。

    你需要实现类似的东西:

    def do_stuff(queue, request = nil)
      request ||= queue.pop
      return unless (request)
    
      conn = EM::Protocols::HttpClient2.connect request.host, 80
    
      req = conn.get(request.query)
      req.callback { |response|
        p(response.status)
        p(response.headers)
        p(response.content)
    
        EventMachine.next_tick do
          # This schedules an operation to be performed the next time through
          # the event-loop. Usually this is almost immediate.
          do_stuff(queue)
        end
      }
    end   
    

    在你的事件循环中,你踢出这条链:

    EventMachine.run do
      queue = [ ... ] # List of things to do
      do_stuff(queue)
    end
    

    一旦您更好地了解 EventMachine 的工作原理,您可能会找到一种更优雅的方式来实现它。

    【讨论】:

    • 我正在寻找的确切答案。非常感谢!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-07-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多