Advertisement
Guest User

Untitled

a guest
Aug 14th, 2022
262
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.08 KB | None | 0 0
  1. import cluster from 'cluster'
  2. let arr = []
  3.  
  4. function genRandomString(ts) {
  5. let i = 16 * 1024
  6. while (i--) {
  7. arr.push(Date.now().toString().repeat(79))
  8. }
  9. if (arr.length > i * 2 ) {
  10. arr = []
  11. // arr.length = Math.trunc(Math.random() * arr.length)
  12. }
  13. setTimeout(() => genRandomString(ts), ts)
  14. }
  15.  
  16. if (cluster.isPrimary) {
  17. genRandomString(200)
  18. console.log("fork cluster!")
  19. const arr = []
  20. let i = 11
  21. while (i--) {
  22. const work = cluster.fork()
  23. arr.push(work)
  24. }
  25. cluster.on("message", (work, message) => {
  26. work._tt = message
  27. })
  28. setInterval(() => {
  29. const tArr = arr.map(w => [w.process.pid, w._tt])
  30. if (tArr.some(item => item[1] !== tArr[0][1]))
  31. console.log(tArr.join(", "))
  32. }, 5000)
  33. } else {
  34. console.log(new Date().toLocaleTimeString() + " fork" + process.pid)
  35. setInterval(() => {
  36. process.send(new Date().toLocaleTimeString())
  37. // console.log(new Date().toLocaleTimeString() + " " + process.pid)
  38. }, 2000)
  39. genRandomString(200)
  40. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement