Skip to content

Instantly share code, notes, and snippets.

@naholyr naholyr/run.js
Created Jun 21, 2018

Embed
What would you like to do?
Async pool
const { promisify } = require('util')
const delay = promisify(setTimeout)
const pad = (s, len = 4) => s.length >= len ? s : pad(`0${s}`, len)
// 300 tasks (id for log, run for action)
const tasks = []
for (let i = 0; i < 300; i++) {
const id = String(i)
const run = () => delay(2000 + Math.random() * 8000)
tasks.push({ id, run })
}
// Start a worker which will run tasks in series
// take a task, work, take next task, work, …, until there is no task
const getWorkerId = () => Math.ceil(Math.random() * 1000000).toString(36)
const worker = async () => {
const workerId = getWorkerId()
let nTasks = 0
console.log(`[${pad(workerId)}] Started worker…`)
while (tasks.length > 0) {
const task = tasks.shift()
console.log(`[${pad(workerId)}] [${pad(task.id)}] Next task…`)
await task.run()
nTasks++
}
console.log(`[${pad(workerId)}] Terminated worker…`)
return nTasks
}
// Start 8 workers asynchronously
Promise.all([
worker(),
worker(),
worker(),
worker(),
worker(),
worker(),
worker(),
worker(),
])
.then(console.log)
❯ node async-pool.js
[k8kg] Started worker…
[k8kg] [0000] Next task…
[icif] Started worker…
[icif] [0001] Next task…
[e1cd] Started worker…
[e1cd] [0002] Next task…
[k748] Started worker…
[k748] [0003] Next task…
[glkq] Started worker…
[glkq] [0004] Next task…
[92sw] Started worker…
[92sw] [0005] Next task…
[7vn6] Started worker…
[7vn6] [0006] Next task…
[5gwf] Started worker…
[5gwf] [0007] Next task…
[e1cd] [0008] Next task…
[icif] [0009] Next task…
[7vn6] [0010] Next task…
[92sw] [0011] Next task…
[k748] [0012] Next task…
[glkq] [0013] Next task…
[e1cd] [0014] Next task…
[5gwf] [0015] Next task…
[7vn6] [0016] Next task…
[k8kg] [0017] Next task…
[icif] [0018] Next task…
[e1cd] [0019] Next task…
[5gwf] [0020] Next task…
[k748] [0021] Next task…
[7vn6] [0022] Next task…
[k748] [0023] Next task…
[glkq] [0024] Next task…
[92sw] [0025] Next task…
[7vn6] [0026] Next task…
[e1cd] [0027] Next task…
[icif] [0028] Next task…
[k8kg] [0029] Next task…
[5gwf] [0030] Next task…
[k748] [0031] Next task…
[icif] [0032] Next task…
[92sw] [0033] Next task…
[5gwf] [0034] Next task…
[glkq] [0035] Next task…
[icif] [0036] Next task…
[5gwf] [0037] Next task…
[e1cd] [0038] Next task…
[icif] [0039] Next task…
[7vn6] [0040] Next task…
[e1cd] [0041] Next task…
[k8kg] [0042] Next task…
[glkq] [0043] Next task…
[92sw] [0044] Next task…
[k748] [0045] Next task…
[5gwf] [0046] Next task…
[7vn6] [0047] Next task…
[92sw] [0048] Next task…
[k8kg] [0049] Next task…
[5gwf] [0050] Next task…
[7vn6] [0051] Next task…
[icif] [0052] Next task…
[k748] [0053] Next task…
[92sw] [0054] Next task…
[k8kg] [0055] Next task…
[glkq] [0056] Next task…
[e1cd] [0057] Next task…
[5gwf] [0058] Next task…
[k748] [0059] Next task…
[7vn6] [0060] Next task…
[e1cd] [0061] Next task…
[icif] [0062] Next task…
[glkq] [0063] Next task…
[92sw] [0064] Next task…
[k8kg] [0065] Next task…
[e1cd] [0066] Next task…
[7vn6] [0067] Next task…
[k748] [0068] Next task…
[5gwf] [0069] Next task…
[7vn6] [0070] Next task…
[icif] [0071] Next task…
[92sw] [0072] Next task…
[e1cd] [0073] Next task…
[5gwf] [0074] Next task…
[k8kg] [0075] Next task…
[7vn6] [0076] Next task…
[glkq] [0077] Next task…
[5gwf] [0078] Next task…
[icif] [0079] Next task…
[k8kg] [0080] Next task…
[k748] [0081] Next task…
[7vn6] [0082] Next task…
[e1cd] [0083] Next task…
[5gwf] [0084] Next task…
[92sw] [0085] Next task…
[k748] [0086] Next task…
[glkq] [0087] Next task…
[icif] [0088] Next task…
[k8kg] [0089] Next task…
[e1cd] [0090] Next task…
[glkq] [0091] Next task…
[5gwf] [0092] Next task…
[7vn6] [0093] Next task…
[92sw] [0094] Next task…
[k748] [0095] Next task…
[7vn6] [0096] Next task…
[icif] [0097] Next task…
[e1cd] [0098] Next task…
[92sw] [0099] Next task…
[k8kg] [0100] Next task…
[5gwf] [0101] Next task…
[glkq] [0102] Next task…
[7vn6] [0103] Next task…
[k748] [0104] Next task…
[5gwf] [0105] Next task…
[glkq] [0106] Next task…
[e1cd] [0107] Next task…
[icif] [0108] Next task…
[92sw] [0109] Next task…
[k8kg] [0110] Next task…
[e1cd] [0111] Next task…
[k748] [0112] Next task…
[7vn6] [0113] Next task…
[92sw] [0114] Next task…
[k8kg] [0115] Next task…
[92sw] [0116] Next task…
[5gwf] [0117] Next task…
[glkq] [0118] Next task…
[k748] [0119] Next task…
[icif] [0120] Next task…
[7vn6] [0121] Next task…
[5gwf] [0122] Next task…
[e1cd] [0123] Next task…
[glkq] [0124] Next task…
[k8kg] [0125] Next task…
[e1cd] [0126] Next task…
[92sw] [0127] Next task…
[glkq] [0128] Next task…
[7vn6] [0129] Next task…
[icif] [0130] Next task…
[k748] [0131] Next task…
[5gwf] [0132] Next task…
[92sw] [0133] Next task…
[e1cd] [0134] Next task…
[icif] [0135] Next task…
[92sw] [0136] Next task…
[k8kg] [0137] Next task…
[5gwf] [0138] Next task…
[7vn6] [0139] Next task…
[k748] [0140] Next task…
[glkq] [0141] Next task…
[5gwf] [0142] Next task…
[e1cd] [0143] Next task…
[92sw] [0144] Next task…
[icif] [0145] Next task…
[7vn6] [0146] Next task…
[k8kg] [0147] Next task…
[glkq] [0148] Next task…
[k748] [0149] Next task…
[5gwf] [0150] Next task…
[92sw] [0151] Next task…
[k748] [0152] Next task…
[5gwf] [0153] Next task…
[icif] [0154] Next task…
[7vn6] [0155] Next task…
[e1cd] [0156] Next task…
[k8kg] [0157] Next task…
[icif] [0158] Next task…
[92sw] [0159] Next task…
[k748] [0160] Next task…
[glkq] [0161] Next task…
[5gwf] [0162] Next task…
[92sw] [0163] Next task…
[k8kg] [0164] Next task…
[7vn6] [0165] Next task…
[e1cd] [0166] Next task…
[icif] [0167] Next task…
[k748] [0168] Next task…
[5gwf] [0169] Next task…
[icif] [0170] Next task…
[k8kg] [0171] Next task…
[7vn6] [0172] Next task…
[92sw] [0173] Next task…
[glkq] [0174] Next task…
[e1cd] [0175] Next task…
[glkq] [0176] Next task…
[icif] [0177] Next task…
[k748] [0178] Next task…
[7vn6] [0179] Next task…
[5gwf] [0180] Next task…
[k8kg] [0181] Next task…
[5gwf] [0182] Next task…
[92sw] [0183] Next task…
[e1cd] [0184] Next task…
[icif] [0185] Next task…
[5gwf] [0186] Next task…
[e1cd] [0187] Next task…
[k748] [0188] Next task…
[glkq] [0189] Next task…
[7vn6] [0190] Next task…
[icif] [0191] Next task…
[k8kg] [0192] Next task…
[k748] [0193] Next task…
[e1cd] [0194] Next task…
[92sw] [0195] Next task…
[5gwf] [0196] Next task…
[k8kg] [0197] Next task…
[k748] [0198] Next task…
[7vn6] [0199] Next task…
[glkq] [0200] Next task…
[icif] [0201] Next task…
[5gwf] [0202] Next task…
[e1cd] [0203] Next task…
[92sw] [0204] Next task…
[7vn6] [0205] Next task…
[5gwf] [0206] Next task…
[k8kg] [0207] Next task…
[k748] [0208] Next task…
[7vn6] [0209] Next task…
[92sw] [0210] Next task…
[icif] [0211] Next task…
[glkq] [0212] Next task…
[k8kg] [0213] Next task…
[e1cd] [0214] Next task…
[k748] [0215] Next task…
[5gwf] [0216] Next task…
[7vn6] [0217] Next task…
[k8kg] [0218] Next task…
[k748] [0219] Next task…
[92sw] [0220] Next task…
[glkq] [0221] Next task…
[k8kg] [0222] Next task…
[5gwf] [0223] Next task…
[icif] [0224] Next task…
[e1cd] [0225] Next task…
[7vn6] [0226] Next task…
[glkq] [0227] Next task…
[k748] [0228] Next task…
[k8kg] [0229] Next task…
[7vn6] [0230] Next task…
[icif] [0231] Next task…
[5gwf] [0232] Next task…
[e1cd] [0233] Next task…
[92sw] [0234] Next task…
[k8kg] [0235] Next task…
[7vn6] [0236] Next task…
[k748] [0237] Next task…
[e1cd] [0238] Next task…
[92sw] [0239] Next task…
[k748] [0240] Next task…
[glkq] [0241] Next task…
[5gwf] [0242] Next task…
[k8kg] [0243] Next task…
[7vn6] [0244] Next task…
[5gwf] [0245] Next task…
[icif] [0246] Next task…
[92sw] [0247] Next task…
[k748] [0248] Next task…
[glkq] [0249] Next task…
[e1cd] [0250] Next task…
[5gwf] [0251] Next task…
[92sw] [0252] Next task…
[7vn6] [0253] Next task…
[k8kg] [0254] Next task…
[icif] [0255] Next task…
[92sw] [0256] Next task…
[5gwf] [0257] Next task…
[icif] [0258] Next task…
[glkq] [0259] Next task…
[k748] [0260] Next task…
[e1cd] [0261] Next task…
[k8kg] [0262] Next task…
[7vn6] [0263] Next task…
[k748] [0264] Next task…
[e1cd] [0265] Next task…
[glkq] [0266] Next task…
[92sw] [0267] Next task…
[icif] [0268] Next task…
[5gwf] [0269] Next task…
[k748] [0270] Next task…
[k8kg] [0271] Next task…
[icif] [0272] Next task…
[7vn6] [0273] Next task…
[glkq] [0274] Next task…
[icif] [0275] Next task…
[7vn6] [0276] Next task…
[e1cd] [0277] Next task…
[5gwf] [0278] Next task…
[92sw] [0279] Next task…
[5gwf] [0280] Next task…
[glkq] [0281] Next task…
[k748] [0282] Next task…
[k8kg] [0283] Next task…
[icif] [0284] Next task…
[92sw] [0285] Next task…
[e1cd] [0286] Next task…
[k8kg] [0287] Next task…
[5gwf] [0288] Next task…
[glkq] [0289] Next task…
[7vn6] [0290] Next task…
[icif] [0291] Next task…
[e1cd] [0292] Next task…
[k748] [0293] Next task…
[k8kg] [0294] Next task…
[92sw] [0295] Next task…
[k748] [0296] Next task…
[e1cd] [0297] Next task…
[glkq] [0298] Next task…
[7vn6] [0299] Next task…
[5gwf] Terminated worker…
[icif] Terminated worker…
[e1cd] Terminated worker…
[k8kg] Terminated worker…
[k748] Terminated worker…
[92sw] Terminated worker…
[7vn6] Terminated worker…
[glkq] Terminated worker…
[ 35, 37, 38, 38, 33, 37, 40, 42 ]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.