Python Forum
Concurrent futures threading running at same speed as non-threading
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Concurrent futures threading running at same speed as non-threading
#1
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.

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())
Reply


Messages In This Thread
Concurrent futures threading running at same speed as non-threading - by billykid999 - May-03-2023, 03:27 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
  threading native_id returning same value for all threads billykid999 2 1,071 May-04-2023, 06:40 AM
Last Post: billykid999
  Trouble with threading and reading variable from a different script Lembas 14 3,159 Apr-26-2023, 11:21 PM
Last Post: Lembas
  using threading.Timer for function korenron 1 1,240 Dec-20-2022, 01:09 PM
Last Post: ndc85430
  [Solved]Help with Threading Extra 7 1,922 Sep-05-2022, 05:29 PM
Last Post: Extra
Question Opencv and threading ethernel 4 156,030 Feb-25-2022, 06:06 PM
Last Post: ethernel
  Inconsistent counting / timing with threading rantwhy 1 1,785 Nov-24-2021, 04:04 AM
Last Post: deanhystad
  Class variables and Multiprocessing(or concurrent.futures.ProcessPoolExecutor) Tomli 5 3,948 Nov-12-2021, 09:55 PM
Last Post: snippsat
  Mult-threading and locking file mr_byte31 4 2,661 Oct-16-2021, 01:54 AM
Last Post: Larz60+
  Matplotlib Animation with Threading peterjv26 4 7,317 Oct-08-2021, 05:51 PM
Last Post: peterjv26
  Tutorials on sockets, threading and multi-threading? muzikman 2 2,159 Oct-01-2021, 08:32 PM
Last Post: muzikman

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020