简体   繁体   中英

How do I configure the concurrency of node.js?

Is there a way to configure the maximum capacity of Node.js? For example, say, I have 5 URLs, but with limited hardware resource, I only want to process 2 at a time. Is there an option that I can set in Node.js, such that I don't need to control it in my code?

urls.txt

https://example.com/1
https://example.com/2
https://example.com/3
https://example.com/4
https://example.com/5

index.js

const readline = require('readline')
const fs = require('fs')

const rl = readline.createInterface({
  input: fs.createReadStream('urls.txt')
})

rl.on('line', (input) => {
  console.log(`Do something with: ${input}`);
})

Is there an option that I can set in Node.js, such that I don't need to control it in my code?

Nope. That's what your code is for.

Alternatively, let the kernel handle it, since you're concerned about system resources.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM