Notice
Recent Posts
Recent Comments
Link
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | 2 | 3 | ||||
4 | 5 | 6 | 7 | 8 | 9 | 10 |
11 | 12 | 13 | 14 | 15 | 16 | 17 |
18 | 19 | 20 | 21 | 22 | 23 | 24 |
25 | 26 | 27 | 28 | 29 | 30 | 31 |
Tags
- angular modal
- scroll
- 셀렉트박스 커스텀
- 앵귤러 모달
- 모달
- ajax 사용 예시
- 스크롤 이벤트 감지
- Router
- 검색
- angular animation
- Ionic modal
- flex-1
- modal
- egov spring ajax 사용 예시
- route
- prisma
- TAILWIND
- mysql if
- summary
- 스크롤 이벤트
- angular button
- 아이오닉 스크롤 이벤트
- angular route
- 앵귤러 애니메이션
- ApexChart
- 호버
- Angular Router
- 옵저버블
- Oracle LISTAGG 사용 예시
- formgroup
Archives
- Today
- Total
깜놀하는 해므찌로
nodeJs worker 활용 예시 본문
반응형
SMALL
// Importing required modules and functions
import fs from 'fs';
import { join } from 'path';
import { exit } from 'process';
import { Worker, isMainThread, workerData } from 'worker_threads';
import { upload } from './upsert';
// Define the directory path for the files
const directory = join(__dirname, '..', process.env.FOLDER_NAME || 'data');
// Read the file names in the directory
const fileNames = fs.readdirSync(directory);
// Define the number of threads to use, defaulting to 4
const THREAD_COUNT = process.env.THREAD_COUNT ? +process.env.THREAD_COUNT : 4;
// The main function that performs the file processing
function main() {
// Check if there are any files to process
if (fileNames.length < 0) {
console.log('파일이 없습니다. 업로드를 종료합니다.'); // Print a message if no files are found and exit
exit(1);
}
// Iterate over each file in the directory
for (const dir of fileNames) {
if (isMainThread) {
const threads = new Set<Worker>();
// Read the content of the current file
const content = fs.readFileSync(join(__dirname, '..', process.env.FOLDER_NAME || 'data', dir), 'utf-8');
// Count the number of lines in the file
const dataLength = content.split('\n').length;
// Create worker threads for parallel processing
for (let i = 0; i < THREAD_COUNT; i++) {
const start = Math.floor(dataLength / THREAD_COUNT) * i;
const end = Math.floor(dataLength / THREAD_COUNT) * (i + 1);
// Create a new worker thread and provide it with necessary data
threads.add(new Worker(__filename, {
workerData: { filename: dir, lines: content.split('\n').slice(start, end) },
}));
}
// Handle events from worker threads
for (const worker of threads) {
worker.on('error', (err) => {
console.error(err);
});
worker.on('message', (message) => {
// Handle the message received from the worker if needed
});
}
} else {
// Invoke the upload function in the worker thread with the provided data
upload(workerData.filename, workerData.lines);
}
}
}
// Call the main function to start the processing
main();
1. 자세한 설명은 주석을 참조해주세요.
반응형
LIST
'IT' 카테고리의 다른 글
Ion-Content Ionic layout 구조 설명 / router 구조 (0) | 2023.08.01 |
---|---|
PostgreSQL Prisma enum 활용 예시 feat.PostgreSQL (0) | 2023.07.31 |
Navicat set auto increment 나비캣 오토 인크리먼트 설정 (0) | 2023.07.29 |
Angular create apps within NX NX 레포 내부에 새로운 프로젝트 추가하기 feat.client (0) | 2023.07.28 |
법정동 자료 DB 테이블 생성 및 입력 예시 feat.Navicat 나비캣 (0) | 2023.07.27 |