In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/02 Report--
It is believed that many inexperienced people have no idea about how to control the number of threads in nodejs. Therefore, this paper summarizes the causes and solutions of the problem. Through this article, I hope you can solve this problem.
1 Design ideas
The idea of the design is relatively simple, that is, add a layer in front of the user and the work_threads module. If the user calls the work_threads module directly, then any number of threads can be created, and there is no way to control it. By adding a layer, the tasks submitted by the user can be cached, and when a thread exits (when the task is finished), a new thread can be started to process the cached task.
2 concrete realization
2.1 implementation of configuration
Define some configurations, such as the maximum number of threads that can be created. With Proxy, you can hijack an object, and you can do something, but not yet.
/ / Thread pool configuration, using Proxy, you can hijack the read and write of attributes and do something
Const CONFIG = new Proxy ({
MAX_THREAD: 3
}, {
Get (obj, key) {
Return obj [key]
}
Set (obj, key, value) {
Obj [key] = value
Return true
}
})
2.2 implementation of control logic
2.2.1 Constructor
The thread pool records the current number of threads and cached task queues.
Constructor () {
/ / Task queue
This._workQueue = []
/ / the number of frontlines
This._count = 0
}
2.2.2 create a new thread
_ newThread (. Rest) {
/ / todo failed to create new processing
Const worker = new Worker (. Rest)
/ / add one to the number of threads
This._count++
/ / after exiting, create a new thread if there is a cached task
Worker.on ('exit', ()) = > {
This._count--
/ / there are places and there are tasks waiting.
If (this._workQueue.length) {
Const {
Resolve
Reject
Params
} = this._workQueue.shift ()
/ / start the thread and notify the user
Resolve (this._newThread (.params))
}
});
Return worker
}
2.2.3 submit a task to the thread pool
/ / submit a task to the thread pool
Submit (. Rest) {
Return new Promise ((resolve, reject) = > {
/ / create a new thread if the threshold has not been reached, otherwise it will be cached
If (this._count
< CONFIG.MAX_THREAD) { resolve(this._newThread(...rest)); } else { this._workQueue.push({resolve, reject, params: rest}); } }); } } 思想很简单,根据配置,最多创建n个线程,如果任务太多的话,就缓存起来,等待有线程退出的时候,再新建一个线程处理缓存起来的任务。API使用Promise方式。这样可以实现等待操作。等到创建线程的时候可以通知用户。而且用户使用的时候,几乎是透明的,没有太多额外的成本,因为只是做了一些封装,几乎是透传nodejs的线程功能。最后提供多种方式调用,包括一个默认的控制器、创建多个控制器。 const defaultThreadGate = new ThreadGate(); module.exports = { ThreadGate, defaultThreadGate, submit: (...rest) =>{
Return defaultThreadGate.submit (. Rest)
}
CONFIG
}
After reading the above, have you mastered the method of how to control the number of threads in nodejs? If you want to learn more skills or want to know more about it, you are welcome to follow the industry information channel, thank you for reading!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.