Help us understand the problem. What is going on with this article?

Node.js スレッドモデルとイベントループとは

More than 1 year has passed since last update.

スレッドモデルとイベントループ

大量のリクエストをさばくための処理形式

スレッドモデル

Requestに対し、Threadを立ち上げて対応
大量に来ると、Request処理待ちが発生する

イベントループ(Node.js)

QueueでEventLoopを回す(メインのスレッド)
 →Requestをためる
I/O(バックグラウンドのスレッド)

メリット:
前の処理が終わるまで待つ必要がない
デメリット:
処理の終了順番が把握できない
ループをブロックしないプログラミング(NON-Blocking)が必要

ノンブロッキングな書き方とは

時間の掛かりそうな処理はコールバック関数で書く

test1.js
//non blocking
setTimeout(function() {
    console.log("hello");
}, 1000);
console.log("world");

結果:helloの処理(表示)を待たずにworldが表示される

test2.js
//blocking
var start = new Date.getDate()
while (new Date.getDate() < start + 1000);
console.log("world");

結果:helloの処理(表示)を待ってworldが表示される

Why not register and get more from Qiita?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
Comments
Sign up for free and join this conversation.
If you already have a Qiita account
Why do not you register as a user and use Qiita more conveniently?
You need to log in to use this function. Qiita can be used more conveniently after logging in.
You seem to be reading articles frequently this month. Qiita can be used more conveniently after logging in.
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away