简体   繁体   中英

Use Threading in my Python code - Simple Ping and NSLOOKUP

I have created a script to run a simple ping and nslookup test and it works fine. The only problem is, it takes huge amount of time if I have lot of devices. One option I came across is to use Threading concept. Unfortunately, after lot of research only thing I realized is that Python beginners and Threading don't go along well. I was hoping if I can use some help and actually see how it works in my code so that I could apply it in my further programs too. I tried using few lines of multiprocessing code in my program but I guess it's not working.

This is my code:

import csv
import subprocess
import socket
from multiprocessing import Pool


class Devices:
    def __init__(self, name):
        self.name = name

    def hostname(self):
        if ".com" in self.name:
            return self.name.split('.')[0]
        else:
            return self.name

    def pingtest(self):
        response = subprocess.Popen(['ping.exe', device.hostname()], stdout=subprocess.PIPE).communicate()[0]
        response = response.decode()
        if 'bytes=32' in response:
            return 'Up'
        else:
            return 'Down'

    def nslookup(self):
        try:
            name = socket.getfqdn(device.hostname())
            return name
        except socket.error:
            return 'Error'


def initializefile(file):
    with open('Book1.csv', 'r', newline='') as i:
        return convertrows(csv.DictReader(i))


def convertrows(rows):
    return [Devices(row['Device_Name']) for row in rows]

file = r"My\Book1.csv"
devices = initializefile(file)

with open('Output_PingTest_Threading.csv', 'w', newline='') as csvoutput:
    fieldnames = ['Device', 'Ping Test', 'NSLOOKUP']
    output = csv.DictWriter(csvoutput, fieldnames=fieldnames)
    output.writeheader()

for device in devices:
    with open('Output_PingTest_Threading.csv', 'a', newline='') as csvoutput:
        output = csv.writer(csvoutput)
        output.writerows([[device.name] + [device.pingtest()] + [device.nslookup()]])
        print("Device: %s" % device.name)
        print("Ping Status: %s" % device.pingtest())
        print("NSLOOKUP: %s\n" % device.nslookup())

if __name__ == '__main__':
    pool = Pool()
    pool.map(device.pingtest(), device.nslookup(), device)
    pool.close()
    pool.join()

Basically, I am only looking to create 2 threads for the 2 functions(pingtest and nslookup), maybe if I could get the hang if it, I can use it in other programs as well.

So I was able to create threads for each of the function and it did reduce the execution time by almost 50%, although I feel this could be reduced to a lot more than this; guys any help is appreciated!

CODE WITHOUT THREADING:

import csv
import subprocess
import time
import socket


class Devices:
    def __init__(self, name):
        self.name = name

    def pingtest(self):
        response = subprocess.Popen(['ping.exe', device.name], stdout=subprocess.PIPE).communicate()[0]
        response = response.decode()
        if 'bytes=32' in response:
            return 'Up'
        else:
            return 'Down'

    def nslookup(self):
        name = socket.getfqdn(device.name)
        return(name)


def initializefile(file):
    with open('List_of_6_Devices.csv', 'r', newline='') as i:
        return convertrows(csv.DictReader(i))


def convertrows(rows):
    return [Devices(row['New Name']) for row in rows]

file = r"My\List_of_6_Devices.csv"
devices = initializefile(file)

_start = time.time()
for device in devices:
    #_start = time.time()
    device.pingtest()
    print("Device: %s" % device.name)
    print("Ping Status: %s" % device.pingtest())
    print("FQDN: %s" % device.nslookup())

print("TOTAL EXECUTION TIME", (time.time() - _start))

OUTPUT:

{PING STATUS OF 6 DEVICE HERE }
TOTAL EXECUTION TIME 41.68950819969177

CODE WITH THREADING:

import threading
import csv
import subprocess
import socket
import time


def ping():
    response = subprocess.Popen(['ping.exe', device], stdout=subprocess.PIPE).communicate()[0]
    response = response.decode()
    if 'bytes=32' in response:
        status = 'Up'
        print("Ping status: %s\n" % status)
    else:
        status = 'Down'
        print("Ping status: %s\n" % status)


def nsloookup():
    name = socket.getfqdn(device)
    print("FQDN: %s" % name)


def initializefile(file):
    with open('List_of_6_Devices.csv', 'r') as f:
        return convertrows(csv.DictReader(f))


def convertrows(rows):
    return [(row['New Name']) for row in rows]


file = r"My\List_of_6_Devices.csv"
devices = initializefile(file)

if __name__ == "__main__":
    # creating thread
    _start = time.time()
    for device in devices:
        t1 = threading.Thread(target=ping)
        t2 = threading.Thread(target=nsloookup())

    # starting thread 1
        t1.start()
    # starting thread 2
        t2.start()

    # wait until thread 1 is completely executed
        t1.join()
    # wait until thread 2 is completely executed
        t2.join()

    # both threads completely executed
    print("TOTAL EXECUTION TIME", (time.time() - _start))

OUTPUT:

{PING STATUS OF 6 DEVICES}
TOTAL EXECUTION TIME 24.59475827217102

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM