Reputation: 2322
When '/a' is called, '/b' can be executed immediately. But If I invokes another '/a', the second one waits for the first one ending. How can I make '/a' really async called?
code:
app.get '/a', (req, res, next) ->
f = ->
res.send 'a'
console.log 'end', new Date()
console.log 'sleep', new Date()
setTimeout f, 10000
app.get '/b', (req, res, next) ->
res.send 'b'
output:
Express server listening on port 3000 in development mode
sleep Sun Oct 14 2012 12:37:52 GMT+0800 (SGT)
GET /b 200 9ms - 1
end Sun Oct 14 2012 12:38:02 GMT+0800 (SGT)
GET /a 200 10022ms - 1
sleep Sun Oct 14 2012 12:38:02 GMT+0800 (SGT)
end Sun Oct 14 2012 12:38:12 GMT+0800 (SGT)
GET /a 200 10005ms - 1
Upvotes: 1
Views: 951
Reputation: 2322
I got the reason, it is because I ran two '/a' on the same browser. I just tried to run one in chromium, and the other one in firefox, they were handled asyncly. Looks interesting.
Upvotes: 1
Reputation: 2322
Bill, I got different result with your code. I ran these via browser. I am using Express 3 on Linux with only one core.
express:application booting in development mode +0ms
connect:dispatcher use / query +0ms
connect:dispatcher use / expressInit +0ms
connect:dispatcher use / router +2ms
express:router defined get /a +0ms
express:router defined get /b +1ms
connect:dispatcher query +12s
connect:dispatcher expressInit +2ms
connect:dispatcher router +0ms
express:router dispatching GET /a (/a) +12s
express:router matched get /a +0ms
sleep Sun Oct 14 2012 13:20:37 GMT+0800 (SGT)
connect:dispatcher query +3s
connect:dispatcher expressInit +0ms
connect:dispatcher router +0ms
express:router dispatching GET /b (/b) +3s
express:router matched get /b +1ms
end Sun Oct 14 2012 13:20:47 GMT+0800 (SGT)
connect:dispatcher query +6s
connect:dispatcher expressInit +0ms
connect:dispatcher router +0ms
express:router dispatching GET /a (/a) +6s
express:router matched get /a +0ms
sleep Sun Oct 14 2012 13:20:47 GMT+0800 (SGT)
end Sun Oct 14 2012 13:20:57 GMT+0800 (SGT)
Upvotes: 0
Reputation: 25565
I'm not sure what you're seeing. When I run the test, I get the expected results - the requests to \a
are handled asynchronously. If you try the following code and execute it with DEBUG=* node app.js
do you get the same results that I do?
var express = require('express'),
app = express();
app.get('/a', function (req, res, next) {
var f = function () {
res.send('a');
console.log('end', new Date());
};
console.log('sleep', new Date());
setTimeout(f, 10000);
});
app.get('/b', function (req, res, next) {
res.send('b');
});
app.listen(4000);
Here is the output from running two requests to \a
and a request to \b
while the first two are sleeping.
express:router dispatching GET /a (/a) +52s // first call to \a
express:router matched get /a +1ms
sleep Sat Oct 13 2012 21:59:42 GMT-0700 (PDT)
connect:dispatcher query +530ms
connect:dispatcher expressInit +1ms
connect:dispatcher router +0ms
express:router dispatching GET /a (/a) +530ms // second call to \a in parallel
express:router matched get /a +0ms
sleep Sat Oct 13 2012 21:59:42 GMT-0700 (PDT)
connect:dispatcher query +874ms
connect:dispatcher expressInit +0ms
connect:dispatcher router +0ms
express:router dispatching GET /b (/b) +874ms // call to \b handled immediately
express:router matched get /b +0ms
end Sat Oct 13 2012 21:59:52 GMT-0700 (PDT) // first call to \a ends
end Sat Oct 13 2012 21:59:52 GMT-0700 (PDT) // second call ends at same time
You can see that the request to \b
finishes immediately and then the two requests to \a
both finish after 10s, meaning that they were, in fact, handled in parallel (as is expected).
Upvotes: 0