Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Node.jsのWorker threadsの話

nus3
June 17, 2024

Node.jsのWorker threadsの話

nus3

June 17, 2024
Tweet

More Decks by nus3

Other Decks in Technology

Transcript

  1. Vitest runs tests in pools. By default, there are several

    poolsÇ 0 `threads` to run tests using `node:worker_threads` (isolation is provided with a new worker context 0 `forks` to run tests using `node:child_process` (isolation is provided with a new `child_process.fork` process) https://github.com/vitest-dev/vitest/blob/471cf97b0c00621d74bf9b880d02e293146b2290/docs/advanced/pool.md?plain=1#L9 Q: なぜWorker threads?
  2. Worker threadsとは The node:worker_threads module enables the use of threads

    that execute JavaScript in parallel. node:worker_threadsモジュールを使うことで、 JavaScriptを するスレッドを作成できる 並列で実行 https://nodejs.org/api/worker_threads.html#worker-threads
  3. By default Vitest runs test files in multiple threads using

    `node:worker_threads` via Tinypool (a lightweight fork of Piscina), allowing tests to run simultaneously. デフォルトだと、Vitestはnode:worker_threadsを使いマルチスレッ ドで並列にテストを実行する。 https://github.com/vitest-dev/vitest/blob/471cf97b0c00621d74bf9b880d02e293146b2290/docs/guide/features.md?plain=1#L35 意訳ネ Q: Vitestでの並列処理って?
  4. node:worker_threads ./worker.js スレッド間の通信は以下の方法があD P parentPort: 親スレッドとの通0 P MessageChannel: 1対1のスレッド間の通0 P

    BroadcastChannel: 1対多のスレッド間の通信 MessageChannelとBroadcastChannelはHTML Living Standardで仕様が定められている
  5. By default Vitest runs test files in multiple threads using

    `node:worker_threads` via (a lightweight fork of Piscina), allowing tests to run simultaneously. Tinypool https://github.com/vitest-dev/vitest/blob/471cf97b0c00621d74bf9b880d02e293146b2290/docs/guide/features.md?plain=1#L35 ナニモワカラナイ
  6. 2 Vitestではテストを並列で実行するのにWorker threadsが使われて いH 2 Tinypoolを使ってテスト実行に使うスレッドを効率的に管理してい H 2 Worker threadsはWeb

    Workerみたいに実装できH 2 マルチスレッド→worker_thread` 2 マルチプロセス→child_process、cluster 探求まとめ