i am trying to ping a group of ip addresses whether it is up or down . For the purpose i am using 3rd party NODE-PING (npm i ping) library. i have a group of ip blocks as it is below.
const hosts = [
{
type: 'office',
block: '192.1.2',
},
{
type: 'station',
block: '192.1.3',
}
]
and i have got another list which includes last part of ip address as below again.
const controlList = [
{
type: 'camera',
end: '100'
},
{
type: 'printer',
end: '98'
}
]
when i use async await syntax in for loops pings wait lots of time for each to process. but whenever i turn to .then() notation it responses faster.
(async function sendPing(hosts,controlList){
for (const host of hosts) {
for (const clist of controlList) {
ping.promise.probe(host.block + '.' + clist.end).then(function(res){
console.log(res.inputHost, res.alive);
})
}
}
})(hosts,controlList);
i am trying to run code faster using async-await as it runs with .then notation.
i am trying to ping a group of ip addresses whether it is up or down . For the purpose i am using 3rd party NODE-PING (npm i ping) library. i have a group of ip blocks as it is below.
const hosts = [
{
type: 'office',
block: '192.1.2',
},
{
type: 'station',
block: '192.1.3',
}
]
and i have got another list which includes last part of ip address as below again.
const controlList = [
{
type: 'camera',
end: '100'
},
{
type: 'printer',
end: '98'
}
]
when i use async await syntax in for loops pings wait lots of time for each to process. but whenever i turn to .then() notation it responses faster.
(async function sendPing(hosts,controlList){
for (const host of hosts) {
for (const clist of controlList) {
ping.promise.probe(host.block + '.' + clist.end).then(function(res){
console.log(res.inputHost, res.alive);
})
}
}
})(hosts,controlList);
i am trying to run code faster using async-await as it runs with .then notation.
Share Improve this question edited Mar 26 at 12:11 DarkBee 15.5k8 gold badges72 silver badges117 bronze badges asked Mar 26 at 11:58 Rıfat Tolga KiranRıfat Tolga Kiran 3573 silver badges12 bronze badges 2- It sounds like you've already found how to improve the performance of the operation? What are you asking? – David Commented Mar 26 at 12:02
- i need to use await notation because i will push all down ip addresses in an array. and at the end of whole process i will send an information e-mail. if i use .then() i cant do this. – Rıfat Tolga Kiran Commented Mar 26 at 12:06
2 Answers
Reset to default 1Presumably you're awaiting on every iteration of the loop? Something like this:
(async function sendPing(hosts,controlList){
for (const host of hosts) {
for (const clist of controlList) {
const res = await ping.promise.probe(host.block + '.' + clist.end);
console.log(res.inputHost, res.alive);
}
}
})(hosts,controlList);
As semantically implied by the use of await
here, this waits for each individual operation. So the operations execute in serial, each one completing before the next one begins.
It sounds like you want to start all of the operations right away and then await all of them in parallel instead. Promise.all()
is for exactly that.
For example:
(async function sendPing(hosts,controlList){
// invoke the operations
const promises = [];
for (const host of hosts) {
for (const clist of controlList) {
// add the promise to an array
promises.push(ping.promise.probe(host.block + '.' + clist.end));
}
}
// await the results
const results = await Promise.all(promises);
// display the results
for (const res of results) {
console.log(res.inputHost, res.alive);
}
})(hosts,controlList);
You can of course do whatever you like with the results, this is just using your existing code as an example. The main point here is to first queue up all of the promises and then await them all, rather than awaiting each one individually.
You can do this by this approach: Send all ping signals and count them. Then simply count or flag them if they finished. Then waits until all signals get finished, then send your email.
function sendPing(hosts,controlList){
var num_sent = 0;
var num_finished = 0;
for (const host of hosts) {
for (const clist of controlList) {
num_sent++;
ping.promise.probe(host.block + '.' + clist.end).then(function(res){
console.log(res.inputHost, res.alive);
num_finished++;
})
}
}
// now wait for all finishing then send email
function check_all_finished(){
if (num_finished == num_sent){
SendEmail();
return;
}
setTimeout(check_all_finished, 1);
}
check_all_finished();
}
function SendEmail(){
// when we reach here, all pings are done
// Write here your send email routine...
}
发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744146741a4560459.html
评论列表(0条)