Benjamin Brinckerhoff
2008-Apr-06 22:59 UTC
[Eventmachine-talk] Adding regular updates for long server calls
All,
I''m working on my first EM application, and I''m stuck. My
client calls
the server, which executes a long-running operation. I would like the
server to be able to send data back to the client as the job executes,
but right now all the data send to send_data is buffered up and sent
when the job is complete. I''ve Googled for solutions and have seen an
answer using EM.next_tick, but this did not work for me. Also, I
noticed in the document SPAWNED_PROCESSES it mentioned that spawned
processes can be used to solve this problem, but I have not yet found
an example. Here is a simplified version of my code
====== server ======require ''eventmachine''
class ExampleServer < EventMachine::Connection
def receive_data(data)
do_job
end
def do_job
(1..10).each do |i|
send_data "." # I want this data to be sent
back to the client before each call to long_operation
long_operation
end
send_done
end
def long_operation
# something that takes awhile
end
def send_done
send_data "done"
end
end
EventMachine::run {
EventMachine::start_server "192.168.1.101", 7777, ExampleServer
}
====== client =====require ''eventmachine''
class ExampleClient < EventMachine::Connection
def receive_data(data)
puts data
end
end
EventMachine::run {
EventMachine::connect "192.168.1.101", 7777, ExampleClient do |c|
c.send_data "go"
end
}
==============
Thanks in advance for any help you can provide.
Ben