2013-07-22 55 views
0
def index 
    p "INDEX, #{Fiber.current.object_id}" # <- #1 
    EventMachine.run { 
    http = EventMachine::HttpRequest.new('http://google.com/').get :query => {'keyname' => 'value'} 

    http.errback { p "Uh oh, #{Fiber.current.object_id}"; EM.stop } # <- #2 
    http.callback { 
     p "#{http.response_header.status}, #{Fiber.current.object_id}" # <- #3 
     p "#{http.response_header}" 
     p "#{http.response}" 

     EventMachine.stop 
    } 
    } 

    render text: 'test1' 
end 

在这段代码中,我预计#1#2#3线获得不同Fiber ID。但是所有光纤物体的ID都是一样的。我试过Thread.current.object_id,但也是一样的结果。
我误解了什么?该代码甚至是异步执行?为什么我的eventmachine客户端代码不能异步工作?

P.S我使用ruby 2.0和代码与rails4

+0

您应该使用em-synchrony来查看您的光纤:) – fl00r

回答

1

http://ruby-doc.org/core-2.0/Fiber.html

Fibers are primitives for implementing light weight cooperative concurrency in Ruby. Basically they are a means of creating code blocks that can be paused and resumed, much like threads. The main difference is that they are never preempted and that the scheduling must be done by the programmer and not the VM.

运行凡在你的代码是你安排的纤维,例如调用Fiber.yield或my_fiber.resume?

current() → fiber
Returns the current fiber. You need to require 'fiber' before using this method. If you are not running in the context of a fiber this method will return the root fiber.

在代码中创建了其他光纤的位置,例如, Fiber.new做...?

Is that code even executes asynchronously?

require 'em-http-request' 
require 'fiber' 

puts Fiber.current.object_id 

def index 
    p "INDEX, #{Fiber.current.object_id}" # <- #1 
    EventMachine.run { 
    http = EventMachine::HttpRequest.new('http://google.com/').get :query => {'keyname' => 'value'} 

    http.errback { p "#{Uh oh}, #{Fiber.current.object_id}"; EM.stop } # <- #2 
    http.callback { 
     p "#{http.response_header.status}, #{Fiber.current.object_id}" # <- #3 
     p "#{http.response_header}" 
     p "#{http.response}" 

     EventMachine.stop 
    } 
    } 

    #render text: 'test1' 
end 

index() 

--output:-- 
2157346420 
"INDEX, 2157346420" 
"301, 2157346420" 
"{\"LOCATION\"=>\"http://www.google.com/?keyname=value\", \"CONTENT_TYPE\"=>\"text/html; charset=UTF-8\", \"DATE\"=>\"Mon, 22 Jul 2013 08:44:35 GMT\", \"EXPIRES\"=>\"Wed, 21 Aug 2013 08:44:35 GMT\", \"CACHE_CONTROL\"=>\"public, max-age=2592000\", \"SERVER\"=>\"gws\", \"CONTENT_LENGTH\"=>\"233\", \"X_XSS_PROTECTION\"=>\"1; mode=block\", \"X_FRAME_OPTIONS\"=>\"SAMEORIGIN\", \"CONNECTION\"=>\"close\"}" 
"<HTML><HEAD><meta http-equiv=\"content-type\" content=\"text/html;charset=utf-8\">\n<TITLE>301 Moved</TITLE></HEAD><BODY>\n<H1>301 Moved</H1>\nThe document has moved\n<A HREF=\"http://www.google.com/?keyname=value\">here</A>.\r\n</BODY></HTML>\r\n" 

都能跟得上。

,这是一个错误:

http.errback { p "#{Uh oh}" ... 
+0

+1,我认为代码运行光纤在EventMachine和em-http-request库中。而你的答案'Nope'尚不清楚,但还不清楚它的工作原理。 'errback'已经修复。 – Benjamin

+0

'我认为代码运行光纤在EventMachine'中。仔细查看代码,然后查看输出中的object_id。 – 7stud

0

作为search of the repository显示,em-http默认不使用纤维。然而,这个链接列出了一个如果你倾向于使用光纤的例子。

相关问题