shuzhiren-comfyui/任务队列后端/worker_threads/wait/GenerateWorkerManager.js

41 lines
1.1 KiB
JavaScript

// wait/WaitWorkerManager.js
import { parentPort } from 'worker_threads';
import GenerateThreadPool from './GenerateThreadPool.js';
// 在独立的 Worker 中初始化线程池
const threadPool = GenerateThreadPool; // 6个线程的线程池
// 处理任务的函数
async function processTasks(tasks) {
// 每100个任务为一组
const batchSize = 100;
const batches = [];
// 将任务数据按批次分割
for (let i = 0; i < tasks.length; i += batchSize) {
batches.push(tasks.slice(i, i + batchSize));
}
try {
await threadPool.executeAllTasks(batches);
} catch (error) {
console.error('线程池处理任务时出错:', error);
}
}
// 监听来自主线程的消息
parentPort.on('message', async (tasks) => {
// // console.log('接收到来自主线程的消息:', tasks);
if (tasks && Array.isArray(tasks) && tasks.length > 0) {
await processTasks(tasks);
parentPort.postMessage({ status: 'completed' });
}
});
// 监听退出消息,清理资源
parentPort.on('message', (message) => {
if (message.type === 'terminate') {
threadPool.terminate();
process.exit(0);
}
});