안녕하세요. 나는 얇은 문서를 읽었으며 이벤트 머신에 합리적으로 새로운 것이지만 Deferrables의 작동 방식을 알고 있습니다. 나의 목표는 시체가 연기되고 부분별로 스트리밍 될 때 Thin이 작동하는 방식을 이해하는 것입니다.씬 (thin) 비동기 앱 예제의 신체 반응은 어떻게 버퍼링됩니까?
다음은 제가 일하고 있고 머리를 쓰려고하는 예입니다. 내가 분명하지 이해
class DeferrableBody
include EventMachine::Deferrable
def call(body)
body.each do |chunk|
@body_callback.call(chunk)
end
# @body_callback.call()
end
def each &blk
@body_callback = blk
end
end
class AsyncApp
# This is a template async response. N.B. Can't use string for body on 1.9
AsyncResponse = [-1, {}, []].freeze
puts "Aysnc testing #{AsyncResponse.inspect}"
def call(env)
body = DeferrableBody.new
# Get the headers out there asap, let the client know we're alive...
EventMachine::next_tick do
puts "Next tick running....."
env['async.callback'].call [200, {'Content-Type' => 'text/plain'}, body]
end
# Semi-emulate a long db request, instead of a timer, in reality we'd be
# waiting for the response data. Whilst this happens, other connections
# can be serviced.
# This could be any callback based thing though, a deferrable waiting on
# IO data, a db request, an http request, an smtp send, whatever.
EventMachine::add_timer(2) do
puts "Timer started.."
body.call ["Woah, async!\n"]
EventMachine::add_timer(5) {
# This could actually happen any time, you could spawn off to new
# threads, pause as a good looking lady walks by, whatever.
# Just shows off how we can defer chunks of data in the body, you can
# even call this many times.
body.call ["Cheers then!"]
puts "Succeed Called."
body.succeed
}
end
# throw :asynC# Still works for supporting non-async frameworks...
puts "Async REsponse sent."
AsyncResponse # May end up in Rack :-)
end
end
# The additions to env for async.connection and async.callback absolutely
# destroy the speed of the request if Lint is doing it's checks on env.
# It is also important to note that an async response will not pass through
# any further middleware, as the async response notification has been passed
# right up to the webserver, and the callback goes directly there too.
# Middleware could possibly catch :async, and also provide a different
# async.connection and async.callback.
# use Rack::Lint
run AsyncApp.new
부분은 call
에서 DeferrableBody 클래스와 each
방법 내에서 발생하는 것입니다.
타이머가 @body_callback에 저장된 블록으로 실행되면 몸체를 전송할 때마다 각각의 데이터 청크가 수신되지만, 해당 블록을 호출 할 때 yield
또는 call
이 호출되면 어떻게됩니까? 전송시 단일 메시지.
나는 무슨 일이 일어나고 있는지 이해하기에 충분할만큼 클로저를 이해하지 못한다고 생각합니다. 이것에 대한 도움을 주시면 감사하겠습니다.
감사합니다.