1

当调用'/a' 时,'/b' 可以立即执行。但是如果我调用另一个'/a',第二个会等待第一个结束。我怎样才能让'/a'真正异步调用?

代码:

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'

输出:

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
4

3 回答 3

1

我明白了,这是因为我在同一个浏览器上运行了两个 '/a'。我只是尝试在 chromium 中运行一个,在 Firefox 中运行另一个,它们是异步处理的。看起来很有趣。

于 2012-10-14T05:52:32.203 回答
0

比尔,我用你的代码得到了不同的结果。我通过浏览器运行这些。我在只有一个内核的 Linux 上使用 Express 3。

  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)
于 2012-10-14T05:23:48.473 回答
0

我不确定你看到了什么。当我运行测试时,我得到了预期的结果——请求\a被异步处理。如果您尝试以下代码并执行它,DEBUG=* node app.js您会得到与我相同的结果吗?

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);

这是在前两个处于睡眠状态时运行两个请求\a和一个请求的输出。\b

  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

您可以看到请求\b立即完成,然后两个请求\a都在 10 秒后完成,这意味着它们实际上是并行处理的(正如预期的那样)。

于 2012-10-14T05:04:58.763 回答