[英]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. 我创建了一个脚本来运行简单的ping和nslookup测试,并且运行正常。 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.
不幸的是,经过大量研究,我唯一意识到的是Python初学者和Threading进行得并不顺利。 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. 基本上,我只希望为2个函数(pingtest和nslookup)创建2个线程,如果可以挂起,也可以在其他程序中使用它。
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; 因此,我能够为每个函数创建线程,并且确实将执行时间减少了近50%,尽管我认为这可以减少很多。 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: 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: OUTPUT:
{PING STATUS OF 6 DEVICES}
TOTAL EXECUTION TIME 24.59475827217102
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.