Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- 'use strict'
- const child_process = require('child_process')
- const os = require('os')
- const cpuCount = os.cpus().length
- const chields = []
- const cheerio = require('cheerio')
- const request = require('request')
- const Util = require('./util/divide')
- const dataset = [
- { category: 'one', ref: 'https://site.com/one' },
- { category: 'two', ref: 'https://site.com/two' },
- { category: 'three', ref: 'https://site.com/three' },
- { category: 'four', ref: 'https://site.com/four' },
- { category: 'five', ref: 'https://site.com/five' },
- { category: 'six', ref: 'https://site.com/six' },
- { category: 'seven', ref: 'https://site.com/seven' },
- { category: 'eight', ref: 'https://site.com/eight' },
- ]
- const tasks = Util.chunkArray(dataset, 1, 5)
- const total = tasks.length
- const res = []
- console.log({ count: cpuCount })
- for (let i = 0; i < cpuCount; i++) {
- const forkedChild = child_process.fork(__dirname + '/child.js')
- chields.push(forkedChild)
- }
- for (const forkedChild of chields) {
- const task = tasks.pop()
- console.log({ task })
- if (!task) return
- forkedChild.send(task)
- forkedChild.on('exit', code => {
- console.log('Chield exited:', code)
- })
- forkedChild.on('error', err => {
- console.log(err)
- })
- forkedChild.on('message', message => {
- //console.log('Message from worker', worker.process.pid, ':', message)
- res.push(message)
- console.log(res.length, '/', total)
- if (res.length === total) {
- // process.exit(0)
- }
- })
- }
- ===================================
- 'use strict'
- process.on('message', msg => {
- console.log(`Child got message from master`, { msg })
- process.exit()
- })
- process.send({ data: 'I sent result to master process' })
Add Comment
Please, Sign In to add comment