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
#8
(May-03-2023, 07:31 AM)buran Wrote: As the below code shows, I was not correct, regarding 1, that you don't execute anything

Actually, I was not correct, regarding 1, so strike that

from concurrent.futures import ThreadPoolExecutor

def add(a, b):
    print('inside add')
    return a + b

def sub(a, b):
    print('inside sub')
    return a - b


def pow(a, b):
    print('inside pow')
    return a ** b
   
functions = [add, sub, pow]
a=9
b=3

with ThreadPoolExecutor(max_workers=5) as executor:
    print('start submit')
    future = [executor.submit(func, a, b) for func in functions]
    print('end submit, get result')
    for result in concurrent.futures.as_completed(future):
        print(result.result())
Output:
start submit inside add inside sub end submit, get result inside pow 6 12 729




One thing I noticed is it looks like I'm trying to execute different threads underneath a loop which iterates over the infile list 1 by one, I could just parse the entire file, and then run the threads, but I want to add additional connection drivers in the future outside of Netmiko, and the program will choose the best driver depending on conditions. I'm not sure if this would have an effect. I tried using a context manager underneath __name__ == __main__ and no improvement. This may all be due to this is the fastest it is going to run. I'm running about 15-20 commands on a device for 5 devices and each device takes up about 5-7 seconds so maybe this is what should be expected, and threading may be better for a production environment with many devices?

Corrction, there are 9 commands per device in a given class, which equates to about a second a command. Maybe this is the best I will get (it take about 32 seconds for 6 devices).


This iteration wheich I was getting the result for the executor.map() which I assume means I was running it properly, doesn't show an improvement in time.

        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))
                for i in inlistx:
                    executor = ThreadPoolExecutor(max_workers=50)
                    res = executor.map(i(ConnectHandler, ip))
                    res.result()
                    executor.shutdown()




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())
    # print(time.perf_counter())
    Connector.connect_parser(inlist)
    print(time.perf_counter())
Reply


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

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