Guest User

Untitled

a guest
May 14th, 2020
29
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.73 KB | None | 0 0
  1. 'use strict'
  2.  
  3. const child_process = require('child_process')
  4. const os = require('os')
  5.  
  6. const cpuCount = os.cpus().length
  7. const chields = []
  8.  
  9. const cheerio = require('cheerio')
  10. const request = require('request')
  11.  
  12. const Util = require('./util/divide')
  13.  
  14. const dataset = [
  15. { category: 'one', ref: 'https://site.com/one' },
  16. { category: 'two', ref: 'https://site.com/two' },
  17. { category: 'three', ref: 'https://site.com/three' },
  18. { category: 'four', ref: 'https://site.com/four' },
  19. { category: 'five', ref: 'https://site.com/five' },
  20. { category: 'six', ref: 'https://site.com/six' },
  21. { category: 'seven', ref: 'https://site.com/seven' },
  22. { category: 'eight', ref: 'https://site.com/eight' },
  23. ]
  24.  
  25. const tasks = Util.chunkArray(dataset, 1, 5)
  26. const total = tasks.length
  27. const res = []
  28.  
  29. console.log({ count: cpuCount })
  30.  
  31. for (let i = 0; i < cpuCount; i++) {
  32. const forkedChild = child_process.fork(__dirname + '/child.js')
  33. chields.push(forkedChild)
  34. }
  35.  
  36. for (const forkedChild of chields) {
  37. const task = tasks.pop()
  38. console.log({ task })
  39. if (!task) return
  40. forkedChild.send(task)
  41.  
  42. forkedChild.on('exit', code => {
  43. console.log('Chield exited:', code)
  44. })
  45.  
  46. forkedChild.on('error', err => {
  47. console.log(err)
  48. })
  49.  
  50. forkedChild.on('message', message => {
  51. //console.log('Message from worker', worker.process.pid, ':', message)
  52. res.push(message)
  53. console.log(res.length, '/', total)
  54. if (res.length === total) {
  55. // process.exit(0)
  56. }
  57. })
  58. }
  59.  
  60. ===================================
  61. 'use strict'
  62.  
  63. process.on('message', msg => {
  64. console.log(`Child got message from master`, { msg })
  65. process.exit()
  66. })
  67.  
  68. process.send({ data: 'I sent result to master process' })
Add Comment
Please, Sign In to add comment