在多个路由中声明的workerpool仍然可以保持其CPU使用率,而无需关心阈值。

huangapple go评论70阅读模式
英文:

Does workerpool declared in multiple routes can still maintain its cpu usage without caring about threshold

问题

我希望能够使用workerpool来处理CPU密集型任务的Node.js系统,但在多个路由中有一些关于CPU使用情况让我感到困惑。一个场景如下:

route1.js:

const workerpool = require('workerpool');
const pool = workerpool.pool(__dirname + '/job1.js');
pool.exec.......

route2.js:

const workerpool = require('workerpool');
const pool = workerpool.pool(__dirname + '/job2.js');
pool.exec.......

route3.js:

const workerpool = require('workerpool');
const pool = workerpool.pool(__dirname + '/job3.js');
pool.exec.......

当Node.js使用这三个文件时,它们将创建自己的workerpool,由于worker线程的数量和控制由Node.js内部管理,这是否可能会导致阈值问题?以及如何正确使用workerpool,非常感谢。

英文:

I hope to figure out a node.js system with workerpool to handle cpu intensive tasks, but
there is something confused me about the cpu usage in multiple routes.
A scenario is like this:

route1.js:
  const workerpool = require('workerpool');
  const pool = workerpool.pool(__dirname + '/job1.js');
  pool.exec.......
route2.js:
  const workerpool = require('workerpool');
  const pool = workerpool.pool(__dirname + '/job2.js');
  pool.exec.......
route3.js:
  const workerpool = require('workerpool');
  const pool = workerpool.pool(__dirname + '/job3.js');
  pool.exec.......

When the node.js use these three files, they will create their own workerpool, and
since worker_thread number and its control is via node.js internals, is this possible to
create threshold problem? And how to use workerpool in a correct way, thanks a lot.

答案1

得分: 1

我会只使用一个工作池。工作者可以公开多个函数,因此您的工作者可以毫无问题地公开job1、job2和job3。如果您为每个函数创建一个池,您需要考虑这些池可能会相互影响...

考虑一下,如果您的池子占用了100%的CPU,这意味着如果所有3个池都满了,您最多可以需要300%。

如果您将它们分配为每个池子33%,这意味着您最多可以需要100%,这是不错的,但如果只有job1在某个时间点需要大量资源,它将只能使用可用资源的33%。

通过使用单一池,您可以达到最多100%,而无需超过100%的可用资源。

英文:

What I'd do is only have one pool of worker. Workers can expose multiple functions so your worker could expose job1, job2 and job3 without problem. If you create a pool for each, you'll need to consider that pools may work against each other...

Consider your pools getting 100% of your CPUs, that means you can require up to 300% what you have available if all 3 pools are full.

If you allocated them 33% each, that means you can require up to 100% which is good, but if only job1 is heavily required at one time it will only be able to use 33% of available resources.

By using a single pool, you can reach up to 100% without requiring more than 100% available.

huangapple
  • 本文由 发表于 2023年5月17日 15:25:27
  • 转载请务必保留本文链接:https://go.coder-hub.com/76269510.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定