本文介绍了Node.js中的后台进程的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在NodeJS应用程序中处理后台进程的好方法是什么?

What is a good aproach to handle background processes in a NodeJS application?

场景:在用户向应用程序发布内容后,我想处理数据,从外部资源请求其他数据等.所有这些都非常耗时,因此我希望将其删除req/res循环.理想的情况是只拥有一排作业,您可以在其中快速转储作业,并且守护程序或任务运行器将始终采用最旧的作业并对其进行处理.

Scenario: After a user posts something to an app I want to crunch the data, request additional data from external resources, etc. All of this is quite time consuming, so I want it out of the req/res loop. Ideal would be to just have a queue of jobs where you can quickly dump a job on and a daemon or task runner will always take the oldest one and process it.

在RoR中,我可以使用延迟作业"之类的方法来完成.此API的Node等效项是什么?

In RoR I would have done it with something like Delayed Job. What is the Node equivalent of this API?

推荐答案

如果您想要轻量级的东西,并且可以在与服务器相同的进程中运行,我强烈建议.它有一个简单的API,可以对您的队列进行精细控制.

If you want something lightweight, that runs in the same process as the server, I highly recommend Bull. It has a simple API that allows for a fine grained control over your queues.

如果您正在寻找可以作为独立工作进程运行的程序,则可以查看 Kue .它可以作为RESTful API服务器运行,甚至为它编写了多个前端应用程序.

If you're looking for something that runs as a standalone worker process, perhaps look into Kue. It can run as a RESTful API server, and even has several front-end apps written for it.

如果您熟悉Ruby的Resque,则有一个名为 Node-resque

If you're familiar with Ruby's Resque, there is a node implementation called Node-resque

Bull,Kue和Node-resque均由 Redis 支持,这在Node.js工作人员队列中无处不在.这三者都能做到RoR的DelayedJob所做的事情,这与您想要的特定功能以及您的API首选项有关.

Bull, Kue and Node-resque are all backed by Redis, which is ubiquitous among Node.js worker queues. All 3 would be able to do what RoR's DelayedJob does, it's matter of specific features that you want, and your API preferences.

这篇关于Node.js中的后台进程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-04 04:47