Two answers:
Lazy answer: just use a blocking record. EM is already sending you discrete chunks of data, not just one giant row. Thus, your implementation example may be slightly off. Are you sure you want to create a new temporary file for each individual fragment that EM you like? However, I will continue, believing that your sample code is working as intended.
Admittedly, the lazy approach depends on the device you are writing, but the simultaneous recording of several large streams to disk will be a major obstacle and you will lose your benefits from having a server anyway. You will just finish working with the juggling disk in all places, IO performance will drop dramatically, and your server will also have performance. Working with a lot of things immediately remains in order with RAM, but as soon as you start working with block devices and scheduling I / O, you will run into performance bottlenecks no matter what you do.
However, I think you might want to make some long writes to disk at the same time that you want low latency responses to other heavy requests other than IO. So maybe a good answer:
Use defer .
require 'rubygems' require 'tempfile' require 'eventmachine' module ExampleServer def receive_data(data) operation = proc do begin f = Tempfile.new('random') f.write(data) ensure f.close end end callback = proc do puts "I wrote a file!" end EM.defer(operation, callback) end end EventMachine::run { EventMachine::start_server "127.0.0.1", 8081, ExampleServer puts 'running example server on 8081' }
Yes, it uses threads. In this case, this is not so bad: you do not need to worry about synchronization between threads, because EM is good enough to handle this for you. If you need an answer, use the callback that will be executed on the main reactor thread when the worker thread completes. In addition, the GIL is a bit of a non-problem for this case, since you are dealing with I / O locking here and not trying to achieve CPU concurrency.
But if you intended to write everything to the same file, you will have to be careful with the delay, since a synchronization problem will arise, since your threads will probably try to write to the same file at the same time.
Fitzsimmons
source share