Python Forum
How to run parallel command (same command -ping) - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: How to run parallel command (same command -ping) (/thread-31432.html)



How to run parallel command (same command -ping) - korenron - Dec-10-2020

Hello ,
I have a code that read data from DB and return a list of IP (around 600 every query )
now I wrote a code that check if the device is online
but he check one by one , and it's about 4 second each so
600 can take ~ 2400 seconds ~ 40 min .....
is there any way to make him run 10 pings every check?
so it will take only ~ 4 min to check them all?

this is what I have now that is working :

def CheckOnLine(hostname):
    DeviceTTl = Getttl(hostname)
    if 200 > DeviceTTl > 0:
        print(hostname, "  TTL -  ", DeviceTTl, '  is up!')
        time.sleep(2)
        return True
    elif DeviceTTl == 250:
        print(hostname + " problem in Device 250")
        return False
    else:
        print(hostname, '  OFFLINE!')
        return False


def Getttl(ip):
    try:
        result = os.popen("ping -n 1 " + ip).read()
    except Exception as e:
        print(e)
        return -1
    else:
        n = result.find("TTL=")
        if n > 0:
            ttl = result[n + 4:]
            print(ttl)
            n = ttl.find("\n")
            if n > 0:
                print(ttl[:n])
                return int(ttl[:n])
    return -1
what I need to add?

Thanks ,


RE: How to run parallel command (same command -ping) - MrBitPythoner - Dec-10-2020

A computer only executes a command one after the other. However, you might be able to speed up the proccess.


RE: How to run parallel command (same command -ping) - MrBitPythoner - Dec-10-2020

https://superuser.com/questions/1586246/how-can-i-do-a-ping-every-500-ms


RE: How to run parallel command (same command -ping) - palladium - Dec-12-2020

Asyncio might be your best bet:

https://docs.python.org/3/library/asyncio.html