← Back to recipes

Run parallel workers

Create queue items and start a worker pool that processes them in parallel browser tabs.

Queues and Workers Intermediate JavaScript parallel_workers.js

Prerequisites

  • An open Guida workspace
  • A worker script saved at scripts/check-site.js

Expected result

Guida enqueues site checks and starts a worker pool with two concurrent browser tabs.

Source example

parallel_workers.js

Workers let Guida process queue items concurrently. Each worker gets its own browser tab and queue item context.

// Parallel Workers — Process queue items concurrently
//
// Workers run queue items in parallel, each in its own
// browser tab. This script sets up a worker pool that
// processes a queue with multiple tabs side by side.
// Requires an open workspace.
const queueName = "sites_to_check";
// First, enqueue some work items
const sites = [
{ url: "https://example.com", name: "Example" },
{ url: "https://httpbin.org", name: "HTTPBin" },
{ url: "https://jsonplaceholder.typicode.com", name: "JSONPlaceholder" }
];
for (const site of sites) {
g.queue.enqueue(queueName, site);
}
g.log("Enqueued " + sites.length + " sites");
// Start a worker pool with 2 concurrent tabs
// Each worker runs 'scripts/check-site.js' per item
g.workers.start({
clearTabs: true,
layout: "vertical",
workers: [{
queue: queueName,
script: "scripts/check-site.js",
concurrency: 2,
throttle: {
requestsPerMinute: 30
}
}]
});
g.log("Worker pool started! Watch the Task Manager pane.");
g.log("Workers will process items independently.");
g.log("Use g.workers.status() to check progress.");
g.log("Use g.workers.stop() to stop the pool.");
// ---- In scripts/check-site.js you would write: ----
//
// // Get the worker context (assigned tab + queue item)
// const ctx = g.worker.getContext();
// const tabOpts = { browserTabId: ctx.tabId };
//
// // Navigate in the worker's own tab
// await g.nav.navigateToUrl(ctx.item.data.url, tabOpts);
//
// // Extract data from the page
// const title = await g.page.executeJS("document.title", tabOpts);
//
// // Save result to the store
// g.store.put("site_checks", ctx.item.data.name, {
// title: title,
// url: ctx.item.data.url,
// checkedAt: new Date().toISOString()
// });
//
// // Commit to remove item from queue (required!)
// g.queue.commit(ctx.item.id);