Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save jeffdev11/b2e57f7a60fe8d56dfd5332aa14c631f to your computer and use it in GitHub Desktop.
Save jeffdev11/b2e57f7a60fe8d56dfd5332aa14c631f to your computer and use it in GitHub Desktop.

Revisions

  1. @midnightcodr midnightcodr revised this gist Jun 14, 2019. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion write-two-millio-rows-of-csv-data-with-node-cluster.js
    Original file line number Diff line number Diff line change
    @@ -36,7 +36,7 @@ function masterProcess () {
    written += 1
    }
    if (written % 100000 === 0) {
    console.log(`sent=${written}`)
    console.log(`written=${written}`)
    }
    if (written >= N) {
    console.log('all done')
  2. @midnightcodr midnightcodr revised this gist Jun 14, 2019. No changes.
  3. @midnightcodr midnightcodr created this gist Jun 14, 2019.
    65 changes: 65 additions & 0 deletions write-two-millio-rows-of-csv-data-with-node-cluster.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,65 @@
    const faker = require('faker')
    // const N = 30
    const N = 2000000
    const fs = require('fs')
    const record = () => {
    // faker.fake(
    // '{{name.lastName}},{{name.lastName}},{{address.city}},{{address.county}},{{address.zipCode}},{{hacker.adjective}}\n'
    return [
    faker.name.firstName(),
    faker.name.lastName(),
    faker.address.city(),
    faker.address.state(),
    faker.address.zipCode(),
    faker.hacker.adjective()
    ]
    }
    const cluster = require('cluster')
    const numCPUs = require('os').cpus().length

    let workers = []
    let written = 0
    if (cluster.isMaster) {
    masterProcess()
    } else {
    childProcess()
    }

    function masterProcess () {
    console.log(`Master ${process.pid} is running`)

    for (let i = 0; i < numCPUs; i++) {
    console.log(`Forking process number ${i}...`)
    const worker = cluster.fork()
    worker.on('message', msg => {
    if (msg === 'done') {
    written += 1
    }
    if (written % 100000 === 0) {
    console.log(`sent=${written}`)
    }
    if (written >= N) {
    console.log('all done')
    process.exit()
    }
    })
    workers.push(worker)
    }
    for (let i = 0; i < N; i++) {
    const workerId = i % numCPUs
    const worker = workers[workerId]
    worker.send(i)
    }
    }

    function childProcess () {
    const writer = fs.createWriteStream('out.csv', { flags: 'a' })
    console.log(`Worker ${process.pid} started`)
    process.on('message', message => {
    const msg = record().join(',') + '\n'
    writer.write(msg, () => {
    // signal writing is done for each entry
    process.send('done')
    })
    })
    }