nodejs工人线程池

发布于 2025-02-06 22:01:53 字数 1348 浏览 0 评论 0原文

在这里对拥有更多工作人员经验的人有一个疑问。.

我一直在使用工人线程进行一些测试,并对他们的工作方式或应该如何工作有疑问。

我正在使用一个名为Piscina的工作线池,这已经设置了,并且似乎正在工作。 “出现”是关键。

这是我的情况。我有一个“ workers.js”文件,该文件具有“更长”的运行脚本(这是用于测试,并且有目的地循环)

运行时,它可以按照应有的措施来处理其他任务等。但是我注意到的是,似乎只有1名工人。

我的意思是,随后对该路线的请求似乎已排队,因此请求不要并行运行,而是等到第一个工人完成,然后继续进行下一个。

我想发生的事情是,每个请求都会从一个新的“工人”(以至于一个点,然后将它们放在Que中),我们将应用程序运行在容器中,因此,如果CPU或MEM太高,它应该将流量推向其他容器等。

现在,我们仍然希望在每个请求中催生工人,以免瓶颈入站请求到该页面。

再说一次,也许我不正确地理解这一点,但是任何与工人有更多经验的人都将不胜感激。

***用代码编辑***

路线: *路由包含Piscina Library

router.get('/:error?', auth("3","edit"),function(req,res){

    console.log('running')


    let piscina = new Piscina({
        filename: path.resolve(__dirname, 'worker.js'),
        minThreads:5
    });

    const result = piscina.run({accountID:req.session.AccountID,cID:req.session.cID,cCode:req.session.cCode}).then(data =>{

Console.log(date)

    })

});

Worker File

module.exports = async({accountID,cID,cCode}) => {
    
    const n = 10000000000;
    for (let i = 1; i <= n; i++) {
        
    }

    return 'finished';
})

的导入循环后,它只需返回“完成的”字符串,如所指出的那样,但是如果我在多个选项卡中点击页面,它们并不全部完成,相反,同一时间说,Tab 1需要7秒才能完成,选项卡2和3 tak 14,然后是21等。

注意:我们传递给工人的变量没有使用RN,这只是一个循环,我们进行测试以验证流量

Have a question for those with more experience with worker threads here..

I have been doing some testing with worker threads and have a question on how they work, or maybe how they should work.

I am using a worker thread pool called piscina, this has been setup and appears to be working. 'Appears' to is the key..

Here is my scenario. I have a 'workers.js' file that has a 'longer' run script ( this is for testing and purposefully a long loop)

When running it, it does what it should, the main EL is still open to process other tasks etc, however what I have noticed is that there only appears to be 1 worker.

What I mean by that, is subsequent request to that route seem to get queued up, so the request don't run in parallel, instead, waits until the first worker finished, then goes on to the next.

What I would like to have happen, is that each request fires off a new 'worker' (to a point, then place them in the que) right now we have the app running in containers, so if the CPU or Mem gets too high, it should push traffic to the other container etc.

Now with that being said, we would still like to have the workers spawn out with each request as to not bottleneck the inbound request to that 'page'.

Again, maybe I'm not understanding this properly but anyone with more experience with workers that would help me out would be greatly appreciated.

*** edit with code ***

Route:
*Route contains imports of piscina library

router.get('/:error?', auth("3","edit"),function(req,res){

    console.log('running')


    let piscina = new Piscina({
        filename: path.resolve(__dirname, 'worker.js'),
        minThreads:5
    });

    const result = piscina.run({accountID:req.session.AccountID,cID:req.session.cID,cCode:req.session.cCode}).then(data =>{

console.log(date)

    })

});

Worker file

module.exports = async({accountID,cID,cCode}) => {
    
    const n = 10000000000;
    for (let i = 1; i <= n; i++) {
        
    }

    return 'finished';
})

After running the loop, it simply return back the 'finished' string, as noted that works, however if I hit the page in multiple tabs they do not all finish at the same time, instead, lets say tab 1 takes 7 seconds to finish, tab 2 and 3 tak 14, then 21 etc.

Note: The variables we are passing to the worker have no use RN, its just a loop that we run for testing to verify the flow

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文