Skip to content

Instantly share code, notes, and snippets.

@Nerian
Last active August 29, 2015 13:58
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Nerian/9942257 to your computer and use it in GitHub Desktop.
Save Nerian/9942257 to your computer and use it in GitHub Desktop.
class ConcurrencyTestApp < Sinatra::Base
get "/asynch_test" do
Net::HTTP.get(URI("http://localhost:5000/long_call"))
"Hello world"
end
get "/long_call" do
sleep 4
end
end
get "/asynch_test" do
Celluloid.defer do
response = Net::HTTP.get(URI("http://localhost:5000/long_call"))
end
"Hello world"
end
Celluloid::Task::TerminatedError
require 'sinatra'
require 'celluloid/io'
require 'http'
class HttpFetcher
include Celluloid::IO
def fetch(url)
HTTP.get(url, socket_class: Celluloid::IO::TCPSocket).response
end
end
class ConcurrencyTestApp < Sinatra::Base
get "/asynch_test" do
fetcher = HttpFetcher.new
response = fetcher.fetch('http://localhost:5001/long_call')
"Hello world"
end
# This is being run on another server
get "/long_call" do
sleep 4
'woow'
end
end
@kenichi
Copy link

kenichi commented Apr 2, 2014

hi, you left #celluloid before i could reply, but if rack is not a requirement, and you like sinatra, i was wondering if you could try my gem "angelo" ? (see github.com/kenichi/angelo) it's still pretty alpha and needs a lot of work but is potentially useful for you -

example:

require 'angelo'
require 'http'

class ConcurrencyTestApp < Angelo::Base

  get '/asynch_test' do
    HTTP.get('http://localhost:5000/long_call', socket_class: Celluloid::IO::TCPSocket).response.body
  end

  get '/long_call' do
    sleep 4
    'hello world'
  end

end

ConcurrencyTestApp.run

then run:

ruby concurrency_test_app.rb -p 5000

@Nerian
Copy link
Author

Nerian commented Apr 2, 2014

@kenichi

That's a pretty good looking gem, good job.

I did this:

require 'angelo'
require 'http'

class ConcurrencyTestApp < Angelo::Base

  get '/asynch_test' do
    HTTP.get('http://localhost:5001/long_call', socket_class: Celluloid::IO::TCPSocket).response.body
  end
end

ConcurrencyTestApp.run

The long_call is running on a different server; so that we can test just as a client.

I execute the code ruby angelo.rb. Then I go to the browser and open two tabs to pointing to 'http://localhost:4567/asynch_test'. The first tab finished after 4 seconds. The second tab finishes after 8 seconds. So they are being handled sequentially.

I am having the same issue pure Reel. Not sure what I should be doing.

@kenichi
Copy link

kenichi commented Apr 2, 2014

@Nerian, thanks! it's good to have use cases to test with! :)

So i tried this: https://gist.github.com/kenichi/9944480

and it seems to work... lemme know what you find!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment