Last active
October 7, 2017 04:09
-
-
Save ericterpstra/e5282020532f00f16bccc1202372cc33 to your computer and use it in GitHub Desktop.
spawn in batches with async/await and stuff.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const { spawn } = require('child_process'); | |
// Search for each the following terms in a file called 'words.txt' | |
const SEARCH_TERMS = "at quis risus sed vulputate odio"; | |
// Split the terms into an array. | |
const searchArray = SEARCH_TERMS.split(" "); | |
// Create an array of 'batches' with 2 terms each. | |
let batches = []; | |
while ( searchArray.length > 0) { | |
batches.push(searchArray.splice(0,2)); | |
} | |
// A function to process the batch concurrently. | |
// Returns a Promise that is resolved when all spawned processes have finished. | |
function processBatch(batch) { | |
// Get an array of promises for each spawned search process. | |
return Promise.all(batch.map( term => { | |
// Return a promise that resolves when term is searched. | |
return new Promise( (resolve, reject) => { | |
let grep = spawn("grep",[term,'words.txt']); | |
grep.stdout.on('data', (data) => { | |
console.log("Search term: ", term, "\r\n__________"); | |
console.log(data.toString()); | |
resolve(); | |
}); | |
grep.stderr.on('data', (data) => { | |
reject(`grep stderr: ${data}`); | |
}); | |
}); | |
})); | |
} | |
// Asynchronously process each batch. | |
batches.forEach( async ( batch, i ) => { | |
console.log("Queueing Batch ", i); | |
// Each batch resolves (finishes) only when all spawned processes have resolved or rejected. | |
await processBatch(batch); | |
console.log('Processed Batch: ', i+1, "\r\n"); | |
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Lectus | |
urnaduis | |
convallis | |
convallis | |
tellus | |
id. | |
Eget | |
nullam | |
non | |
nisi | |
est. | |
Lacinia | |
quis | |
vel | |
eros | |
donec | |
ac. | |
Diam | |
ut | |
venenatis | |
tellus | |
in | |
metus | |
vulputate. | |
Malesuada | |
proin | |
libero | |
nunc | |
consequat | |
interdum | |
varius. | |
Vitae | |
ultricies | |
leo | |
integer | |
malesuada | |
nunc | |
vel | |
risus. | |
Leo | |
in | |
vitae | |
turpis | |
massa. | |
Auctor | |
augue | |
mauris | |
augue | |
neque | |
gravida | |
in | |
fermentum | |
et | |
sollicitudin. | |
Tempor | |
orci | |
dapibus | |
ultrices | |
in | |
iaculis | |
nunc | |
sed | |
augue | |
lacus. | |
Ut | |
placerat | |
orci | |
nulla | |
pellentesque | |
dignissim. | |
Interdum | |
posuere | |
lorem | |
ipsum | |
dolor | |
sit | |
amet | |
consectetur | |
adipiscing | |
elit. | |
At | |
lectus | |
urna | |
duis | |
convallis | |
convallis | |
tellus | |
id. | |
Tristique | |
magna | |
sit | |
amet | |
purus | |
gravida | |
quis | |
blandit | |
turpis. | |
Sollicitudin | |
aliquam | |
ultrices | |
sagittis | |
orci | |
a | |
scelerisque | |
purus. | |
Fusce | |
ut | |
placerat | |
orci | |
nulla | |
pellentesque | |
dignissim | |
enim | |
sit | |
amet. | |
Ac | |
odio | |
tempor | |
orci | |
dapibus | |
ultrices | |
in | |
iaculis. | |
Nec | |
sagittis | |
aliquam | |
malesuada | |
bibendum. | |
Arcu | |
non | |
sodales | |
neque | |
sodales | |
ut | |
etiam | |
sit | |
amet | |
nisl. |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment