May-03-2023, 03:27 AM
I''m doing I/O reads from network devices using netmiko. using a threading context runs the exact same speed as non threading, I'm not sure if it is because I am only testing on 5 devices. I'm running enough commands against the devices, with the time it takes for these I/O operations to execute i would've assumed the threading would've ran faster.
Here is the code without threading:
The sys.argv[1] is a list of classes i'm calling against the devices via the CLI. The Connector.connect_parser(2nd to last line) iterates over a list of ip addresses, after the inlist of outside classes are fed into it.
Now here is the if __name__ == __main__
with the threading context
I reloaded the json file under main, and at the bottom executer the main program with both threads and non threads.
Either i am not using threading right, or 5 devices at this point is not showing a difference.
Any thoughts? I'm sorry to keep running to this forum with what may seem like trivial questions. After I get this threading down thie program is getting sent out to employers, and it's been a drag thus far to fix everything starting at the screen for hours on end.
Here is the code without threading:
The sys.argv[1] is a list of classes i'm calling against the devices via the CLI. The Connector.connect_parser(2nd to last line) iterates over a list of ip addresses, after the inlist of outside classes are fed into it.
if __name__ == "__main__": from Connect_Handler import Hardware, OSPF from Connect_Handler import Connect inlist = sys.argv[1] outlist = inlist.split(",") result = [i.strip("[]") for i in outlist] inlist = [] for i in result: inlist.append(functions[i]) Connector = Connect() print(time.perf_counter()) Connector.connect_parser(inlist) print(time.perf_counter())Here is the main calling program, which applies classes to execute against my ip/device list. I realize there's a bit of redundant code in defining variables more than once , I kept it in there to remind myself I can use the json_loads to override other connection drivers which I will be adding.
class Connect: intake_file = open('ip_list.txt', 'r') json_data = [json.loads(line) for line in intake_file] def __init__(self,): # self.protocol = netmiko.ssh_autodetect pass def connect_parser(self, inlist=[]): for data in self.json_data: ip = data["ip"] # port = data["port"] username = data["username"] if data["username"] else "" password = data["password"] if data["password"] else "" secret = data["secret"] if "secret" in data else False device_type = data["device_type"] if data["device_type"] else "" if data["header"] == "Netmiko": print("The variables being passed: " + ip, username, password, device_type) ConnectHandler = netmiko.ConnectHandler( device_type=device_type, host=ip, username=username, password=password, port=22, secret=data["secret"] if "secret" in data else False ) if ConnectHandler: try: ConnectHandler.enable() except Exception as e: print("Could not connect to {}".format(ip)) #Hardware(ConnectHandler,ip) for i in inlist: i(ConnectHandler, ip)Notice the for i in inlist referes to the sys.argv[1] list
Now here is the if __name__ == __main__
with the threading context
I reloaded the json file under main, and at the bottom executer the main program with both threads and non threads.
Either i am not using threading right, or 5 devices at this point is not showing a difference.
Any thoughts? I'm sorry to keep running to this forum with what may seem like trivial questions. After I get this threading down thie program is getting sent out to employers, and it's been a drag thus far to fix everything starting at the screen for hours on end.
if __name__ == "__main__": from Connect_Handler import Hardware, OSPF from Connect_Handler import Connect intake_file = open('ip_list.txt', 'r') json_data = [json.loads(line) for line in intake_file] for data in json_data: ip = data["ip"] # port = data["port"] username = data["username"] if data["username"] else "" password = data["password"] if data["password"] else "" secret = data["secret"] if "secret" in data else False device_type = data["device_type"] if data["device_type"] else "" inlist = sys.argv[1] outlist = inlist.split(",") result = [i.strip("[]") for i in outlist] inlist = [] for i in result: inlist.append(functions[i]) Connector = Connect() print(time.perf_counter()) with concurrent.futures.ThreadPoolExecutor(max_workers=2) as executor: executor.map(Connector.connect_parser(inlist), data["ip"]) print(time.perf_counter()) print(time.perf_counter()) Connector.connect_parser(inlist) print(time.perf_counter())