1

'/ a'が呼び出されると、'/b'をすぐに実行できます。しかし、別の'/ a'を呼び出すと、2番目のものは最初のものが終了するのを待ちます。'/ 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

理由はわかりました。同じブラウザで 2 つの「/a」を実行したためです。1 つをクロムで、もう 1 つを Firefox で実行しようとしたところ、非同期で処理されました。面白そう。

于 2012-10-14T05:52:32.203 に答える
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);

以下は、最初の 2 つがスリープしている間にへの 2 つのリクエストと へ\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リクエストがすぐに終了し、2 つのリクエストが両方とも 10 秒後に終了したことがわかります。これ\aは、実際には (予想どおり) 並行して処理されたことを意味します。

于 2012-10-14T05:04:58.763 に答える
0

ビル、私はあなたのコードで異なる結果を得ました。これらをブラウザ経由で実行しました。コアが 1 つしかない 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 に答える