簡體   English   中英

NodeJS HTTP 請求隊列

[英]NodeJS HTTP Request Queue

我使用 puppeteer 和 node js (express) 創建了刮板。 這個想法是當服務器收到 http 請求時,我的應用程序將開始抓取頁面。

問題是我的應用程序是否一次收到多個 http 請求。 抓取過程將一遍又一遍地開始,直到沒有 http 請求命中。 我如何只啟動一個 http 請求並將另一個請求排隊,直到第一個抓取過程完成?

目前,我已經嘗試過使用以下代碼的節點請求隊列,但沒有成功。

 var express = require("express"); var app = express(); var reload = require("express-reload"); var bodyParser = require("body-parser"); const router = require("./routes"); const RequestQueue = require("node-request-queue"); app.use(bodyParser.urlencoded({ extended: true })); app.use(bodyParser.json()); var port = process.env.PORT || 8080; app.use(express.static("public")); // static assets eg css, images, js let rq = new RequestQueue(1); rq.on("resolved", res => {}) .on("rejected", err => {}) .on("completed", () => {}); rq.push(app.use("/wa", router)); app.listen(port); console.log("Magic happens on port " + port);

node-request-queue是為request包創建的,不同於express

您可以使用最簡單的承諾隊列庫p-queue來完成隊列 它具有並發支持並且看起來比任何其他庫都更具可讀性。 稍后您可以輕松地從 promise 切換到像bull這樣的健壯隊列。

這是您創建隊列的方法,

const PQueue = require("p-queue");
const queue = new PQueue({ concurrency: 1 });

這是您如何向隊列添加異步函數的方法,如果您收聽它,它將返回已解析的數據,

queue.add(() => scrape(url));

因此,與其將路由添加到隊列中,您只需刪除它周圍的其他線路並保持路由器原樣。

// here goes one route
app.use('/wa', router);

在您的路由器文件之一中,

const routes = require("express").Router();

const PQueue = require("p-queue");
// create a new queue, and pass how many you want to scrape at once
const queue = new PQueue({ concurrency: 1 });

// our scraper function lives outside route to keep things clean
// the dummy function returns the title of provided url
const scrape = require('../scraper');

async function queueScraper(url) {
  return queue.add(() => scrape(url));
}

routes.post("/", async (req, res) => {
  const result = await queueScraper(req.body.url);
  res.status(200).json(result);
});

module.exports = routes;

確保將隊列包含在路由內,而不是相反。 只在你的routes文件或任何你運行爬蟲程序的地方創建一個隊列。

這是scraper文件的內容,你可以使用任何你想要的內容,這只是一個工作假人,

const puppeteer = require('puppeteer');

// a dummy scraper function
// launches a browser and gets title
async function scrape(url){
  const browser = await puppeteer.launch();
  const page = await browser.newPage();
  await page.goto(url);
  const title = await page.title();
  await browser.close();
  return title
}

module.exports = scrape;

使用 curl 的結果:

在此處輸入圖片說明

這是我的 git repo ,其中包含帶有示例隊列的工作代碼。

警告

如果您使用任何此類隊列,您會注意到您在同時處理 100 個結果時遇到問題,並且對您的 api 的請求將持續超時,因為隊列中還有 99 個其他 url 正在等待。 這就是為什么你必須在以后了解更多關於真正的隊列和並發的原因。

一旦您了解了隊列的工作原理,有關 cluster-puppeteer、rabbitMQ、公牛隊列等的其他答案將在那時對您有所幫助:)。

您可以為此使用puppeteer-cluster (免責聲明:我是作者)。 您可以設置一個只有一個工作線程池的集群。 因此,分配給集群的作業將一個接一個地執行。

由於您沒有說明您的 puppeteer 腳本應該做什么,因此在此代碼示例中,我將提取頁面標題作為示例(通過/wa?url=... )並將結果提供給響應。

// setup the cluster with only one worker in the pool
const cluster = await Cluster.launch({
    concurrency: Cluster.CONCURRENCY_CONTEXT,
    maxConcurrency: 1,
});

// define your task (in this example we extract the title of the given page)
await cluster.task(async ({ page, data: url }) => {
    await page.goto(url);
    return await page.evaluate(() => document.title);
});

// Listen for the request
app.get('/wa', async function (req, res) {
    // cluster.execute will run the job with the workers in the pool. As there is only one worker
    // in the pool, the jobs will be run sequentially
    const result = await cluster.execute(req.query.url);
    res.end(result);
});

這是一個最小的例子。 您可能希望捕獲偵聽器中的任何錯誤。 有關更多信息,請查看使用存儲庫中的 express 的屏幕截圖服務器的更復雜示例。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM