WorkerThread

//------------------------------------------------------------------------------------------------worker_threads
const { Worker, isMainThread, parentPort, } = require('worker_threads');

if(isMainThread)
{
    // 부모일 때
    const worker = new Worker(__filename);
    worker.on('message', message => console.log('from worker', message));
    worker.on('exit', () => console.log('worker exit'));
    worker.postMessage('ping');
}

else
{
    parentPort.on('message', (value) => 
    {
        console.log('from parent', value);
        parentPort.postMessage('pong');
        parentPort.close();
    });
}
//------------------------------------------------------------------------------------------------여러 개의 워커 스레드에 데이터를 넘기는 것
const { Worker, isMainThread, parentPort, workerData, } = require('worker_threads');

if(isMainThread)
{
    // 부모일 때
    const threads = new Set();
    threads.add(new Worker(__filename,
    {
        workerData: {start: 1},
    }));
    threads.add(new Worker(__filename,
    {
        workerData: {start: 2},
    }));
    for(let worker of threads)
    {
        worker.on('message', message => console.log('from worker', message));
        worker.on('exit', () => 
        {
            threads.delete(worker);
            if(threads.size == 0)
            {
                console.log('job done');
            }
        });
    }
}

else 
{
    // 워커일 때
    const data = workerData;
    parentPort.postMessage(data.start + 100);
}
//------------------------------------------------------------------------------------------------소수의 개수 구하기_워커 스레드
const min = 2;
const max = 10000000;
const primes = [];

function generatePrimes(start, range)
{
    let isPrime = true;
    const end = start + range;
    for(let i = start; i < end; i++)
    {
        for(let j = min; j < Math.sqrt(end); j++)
        {
            if( i !== j && i % j === 0)
            {
                isPrime = false;
                break;
            }
        }

        if(isPrime)
        {
            primes.push(i);
        }

        isPrime = true;
    }
}

console.time('prime');
generatePrimes(min, max);
console.timeEnd('prime');
console.log(primes.length);
//------------------------------------------------------------------------------------------------workerthread 를 사용하여 소수 개수 구하기
const { Worker, isMainThread, parentPort, workerData } = require('worker_threads');

const min = 2;
let primes = [];

function findPrimes(start, range)
{
    let isPrime = true;
    let end = start + range;
    for(let i = start; i < end; i++)
    {
        for(let j = min; j < Math.sqrt(end); j++)
        {
            if( i !== j && i % j === 0)
            {
                isPrime = false;
                break;
            }
        }

        if(isPrime)
        {
            primes.push(i);
        }
        isPrime = true;
    }
}

if (isMainThread)
{
    const max = 10000000;
    const threadCount = 8;
    const threads = new Set();
    const range = Math.ceil((max - min) / threadCount);
    let start = min;
    console.time('prime');

    for(let i = 0; i < threadCount - 1; i++)
    {
        const wStart = start;
        threads.add(new Worker(__filename, { workerData: { start: wStart, range}}));
        start += range;
    }
    threads.add(new Worker(__filename, { workerData: { start, range: range + ((max - min + 1) % threadCount)}}));

    for(let worker of threads)
    {
        worker.on('error', (err) => 
        {
            throw err;
        });
        worker.on('exit', () =>
        {
            threads.delete(worker);
            if(threads.size === 0)
            {
                console.timeEnd('prime');
                console.log(primes.length);
            }
        });
        worker.on('message', (msg) =>
        {
            primes = primes.concat(msg);
        });
    }
}

else
{
    findPrimes(workerData.start, workerData.range);
    parentPort.postMessage(primes);
}
//-----------------------------------------------------------------------------------------------

REST & 라우팅 사용하기

HTTP 통신