Asynchronous IO Server: Thin (Ruby) and Node.js. Any difference?

I want to clear my concept of asynchronous I / O, a non-blocking server. Dealing with Node.js is easy under the concept

var express = require('express');
var app = express();

app.get('/test', function(req, res){
  setTimeout(function(){
    console.log("sleep doesn't block, and now return");
    res.send('success');
  }, 2000);
});

var server = app.listen(3000, function() {
  console.log('Listening on port %d', server.address().port);
});

I know that when Node.js waits 2 seconds for setTimeout, it can serve another request at the same time, as soon as 2 seconds pass, it will call the callback function.

How about in the world of Ruby, a thin server?

require 'sinatra'
require 'thin'
set :server, %w[thin]

get '/test' do
  sleep 2   <----
  "success"
end

The code snippet above uses a thin server (non-blocking, asynchronous I / O). When you are talking to an asynchronous IO, I want to ask, having reached sleep 2, that the server can serve another request at the same time because it sleep 2blocks the IO.

Node.js sinatra , node.js ( ) ruby ( ? )

, , , , Node.js

+4
3

/

Thin , Sinatra (.. ruby asynchtest.rb)

, ; 2 , .

:

#asynchtest.rb
require 'sinatra'
require 'thin'
set :server, %w[thin]

get '/test' do
  puts "[#{Time.now.strftime("%H:%M:%S")}] logging /test starts on thread_id:#{Thread.current.object_id} \n"
  sleep 10
  "[#{Time.now.strftime("%H:%M:%S")}] success - id:#{Thread.current.object_id} \n"
end

, HTTP- ( timestamp thread-id).

enter image description here , ( cuncurrent), :

  • 70098572502680
  • 70098572602260
  • 70098572485180

( , puts), () ( ).

wikipedia - Asynchronous_I/O: - - -, .

(Sinatra/thin) , curl () Thin () , ( ), , , : *

@Holger: sleep , . , , , , , , node.js: , . /eventmachine .

: "is-sinatra-multi-threaded - concurrency ?

Node.js

, asynchtest.js node.js; asynchtest.rb, , , ; asynchtest.rb:

var express = require('express');
var app = express();

app.get('/test', function(req, res){
  console.log("[" + getTime() + "] logging /test starts\n");
  setTimeout(function(){
    console.log("sleep doen't block, and now return");
    res.send('[' + getTime() + '] success \n');
  },10000);
});

var server = app.listen(3000,function(){
  console.log("listening on port %d", server.address().port);
});

nodejs :
enter image description here

, , .

, , .

+9

, , .

-, " " " ". , Node.js(. ). Ruby EventMachine, .

-, Thin ( Ruby) . , .

  • Ruby 1.8.7 "Thread" . "sleep N" . .
  • Ruby 1.9.x . ( 1000 ..).
  • Ruby 1.9.x " Fibers", , Node.

: Node.js . . Ruby Thread-of ( ..). , , (DNS) .

. Node.JS, JavaScript, , . , , JavaScript , . Ruby (, Metaprogramming).

, Go. : ( Node, ), ( Ruby), ( ). Node Ruby.

0

, node.js , setTimeout script, . , . , .

-1

All Articles