简体   繁体   English

nodejs 中的长时间运行请求

[英]Long running requests in nodejs

I was wondering if there is any good practice how to utilize long-running requests in nodejs?我想知道如何在 nodejs 中利用长时间运行的请求是否有任何好的做法? I am using Express.js and have been wondering if there is any way to, I don't know, move one of express requests to the different instance of node or something?我正在使用 Express.js 并且一直想知道是否有任何方法可以(我不知道)将 express 请求之一移动到节点的不同实例或其他什么东西?

The reason why I am wondering is that if strongly uses a CPU, blocking at the same time event loop in node.我想知道的原因是,如果强烈使用 CPU,同时阻塞 node.js 中的事件循环。 The main problem is that firstly, I am fetching some data, then calculating and inserting missing data into DB, then fetching other data depends on the result, and then calculating again and updating other stuff in DB.主要问题是,首先,我正在获取一些数据,然后计算并将丢失的数据插入到数据库中,然后获取其他数据取决于结果,然后再次计算并更新数据库中的其他内容。

I haven't noticed this problem as I was the only one using this request so far.我没有注意到这个问题,因为到目前为止我是唯一一个使用这个请求的人。 But now I am testing it with other users and if few of them use this request, the whole application freeze for a few seconds.但是现在我正在与其他用户一起测试它,如果很少有人使用这个请求,整个应用程序会冻结几秒钟。 And on my monitoring tools I see a huge CPU usage (100%-200% lol)在我的监控工具上,我看到了巨大的 CPU 使用率(100%-200% 哈哈)

So I have tried clustering but it seems, it does not work.所以我尝试过聚类,但它似乎不起作用。 I have used pm2 and ran app on all cores of my CPU.我已经使用了 pm2 并在我的 CPU 的所有内核上运行了应用程序。 Because of complexicity of my alghoritm I tried to move several functions to worker threads but it looks like these threads would be used very, very often and I am afraid of crashing the whole node instance.由于我的算法的复杂性,我尝试将几个函数移动到工作线程,但看起来这些线程会非常非常频繁地使用,而且我担心整个节点实例崩溃。

I have no clue, which solution would be the best and don't know if are there any good tools for Express, which would help me offload/utilize these requests?我不知道哪个解决方案是最好的,不知道是否有任何好的 Express 工具可以帮助我卸载/利用这些请求? I have not diveed in partitioning yet, but it looks like this may work.我还没有深入研究分区,但看起来这可能有效。 Has anyone used this for the larger projects and knows if this could be the solution to distribute one request into several ticks?有没有人将它用于较大的项目,并且知道这是否可以作为将一个请求分发到多个滴答声中的解决方案?

What about the job queue like Bull or Kue?像 Bull 或 Kue 这样的工作队列呢? Would be this a good idea to push these tasks into a queue?将这些任务推入队列是个好主意吗? I have never used such tools and I am asking, as I have no idea if this would make any sense.我从来没有使用过这样的工具,我在问,因为我不知道这是否有意义。

Cheers干杯

in case you have data present - You can sent data to user using incoming HTTP request.如果您有数据 - 您可以使用传入的 HTTP 请求将数据发送给用户。 but if you don't found any data you can send empty response in HTTP request.但是如果你没有找到任何数据,你可以在 HTTP 请求中发送空响应。

And you need to do calculating and inserting missing data using queue service.并且您需要使用队列服务来计算和插入缺失的数据。 bee-queue is nice one.蜜蜂队列是一个不错的选择。

Use redis with bee-queue to make background jobs faster.使用 redis 和 bee-queue 使后台作业更快。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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