-
-
Save yortz/1140103 to your computer and use it in GitHub Desktop.
Multi-threaded Non blocking file uploads using EventMachine and EM-Http-Request
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env ruby | |
require 'em-http-request' | |
require 'fiber' | |
require "ruby-debug" | |
class AsynUploader | |
@successes = [] | |
@failures = [] | |
def self.async_request(url, file_path) | |
fib = Fiber.current | |
http = EM::HttpRequest.new(url).post :file => file_path | |
http.callback do | |
puts http.response | |
@successes << url | |
fib.resume | |
end | |
http.errback do | |
puts "request failed" | |
@failures << url | |
fib.resume | |
end | |
Fiber.yield | |
end | |
def self.upload_files(path_to_directory) | |
#Dir.chdir(path_to_directory) | |
#files = Dir.glob "*" | |
files = ['ruby', 'rails'] | |
EM.run do | |
Fiber.new{ | |
f = Fiber.current | |
EM::Iterator.new(files, 5).each(proc { |val, iter| Fiber.new {async_request("http://127.0.0.1:9000/"); iter.next; Fiber.yield}.resume} , proc { EM.stop}) | |
}.resume() | |
end | |
end | |
end | |
AsynUploader.upload_files("blah") |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env ruby | |
require 'goliath' | |
require 'yajl' | |
class AsyncUpload < Goliath::API | |
use Goliath::Rack::Params # parse & merge query and body parameters | |
use Goliath::Rack::DefaultMimeType # cleanup accepted media types | |
use Goliath::Rack::Formatters::JSON # JSON output formatter | |
use Goliath::Rack::Render # auto-negotiate response format | |
def on_headers(env, headers) | |
env.logger.info 'received headers: ' + headers.inspect | |
env['async-headers'] = headers | |
end | |
def on_body(env, data) | |
env.logger.info 'received data: ' + data | |
(env['async-body'] ||= '') << data | |
end | |
def on_close(env) | |
env.logger.info 'closing connection' | |
end | |
def response(env) | |
[200, {}, {body: env['async-body'], head: env['async-headers']}] | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment