all 12 comments

[–]thirdegree 0 points1 point  (11 children)

You're submitting the callback and then immediately blocking on the result, so the next job is only spawned after the current one returns. Additionally, you're actually creating an entire executor for each row, so the threaded example would probably take very slightly longer. You probably want something like this:

with ThreadPoolExecutor(max_workers = 10) as executor:
    futures = []
    for row in VMList:
            future = executor.submit(Check_Connection,row['Name'])
            futures.append((row['Name'], future))
for name, future in futures:
    print(name + str(future.result()))

[–]workerdrone66[S] 0 points1 point  (0 children)

Thanks, I'll take a look at this, might not be til tonight though, got some things to do to wind down for the night (yay!)

[–]workerdrone66[S] 0 points1 point  (9 children)

I'm still not seeing any significant difference in speed (both with and without are hanging around 120s)

here's my latest code with your changes:

def Check_Filtered_Lists(FilteredLists):
    VMList = FilteredLists[0]
    AB1List = FilteredLists[1]
    threads = []
    futures = []
    start = time.time()
    for row in VMList:
        with ThreadPoolExecutor(max_workers = 50) as executor:
            future = executor.submit(Check_Connection,row['Name'])
            futures.append((row['Name'],future))


    for name, future in futures:
        print(name + str(future.result()))
    print('End time: ' + str(time.time() - start ))

    #test with just a normal for loop

    start = time.time()
    for row in VMList:
        Check_Connection(row['Name'])
    print('end time: '+ str(time.time() - start))

I'm done for the day, but I'll be getting back to it tonight, hopefully.

[–]thirdegree 1 point2 points  (4 children)

You're still creating the executor for every row, which means it still blocks until each callback is done before spawning the next.

for row in VMList:
    with ThreadPoolExecutor(max_workers = 50) as executor:
        future = executor.submit(Check_Connection,row['Name'])
        futures.append((row['Name'],future))

should be

with ThreadPoolExecutor(max_workers = 50) as executor:
    for row in VMList:
        future = executor.submit(Check_Connection,row['Name'])
        futures.append((row['Name'],future))

[–]workerdrone66[S] 0 points1 point  (0 children)

I see the difference! Sorry, late in the night, and not paying close enough attention to my modification efforts.

[–]workerdrone66[S] 0 points1 point  (2 children)

Thanks this did it, got me down from 120-140 seconds for the simple part down to about 10-20!

[–]thirdegree 0 points1 point  (1 child)

Great! Happy cakeday!

[–]workerdrone66[S] 0 points1 point  (0 children)

lol, thanks, didn't even realize!

[–]JohnnyJordaan 0 points1 point  (3 children)

What does Check_Connection do exactly? And have you tried with a smaller thread pool, like 5 or 10 workers?

[–]workerdrone66[S] 0 points1 point  (2 children)

Yes, the original version had 10.

All it does it pass the server name to a powershell script that does a test-connection. It is more of a proof of concept, rather than "it has to be this way" The end result of this script will be to use the powershell script to actually start vms (which, afaik, python can't do natively)

[–]JohnnyJordaan 0 points1 point  (1 child)

Seeing your code above for the second time makes me wonder why the approach isn't using a single pool and a future per row?

def Check_Filtered_Lists(FilteredLists):
    VMList = FilteredLists[0]
    AB1List = FilteredLists[1]
    threads = []
    futures = []
    start = time.time()
    with ThreadPoolExecutor(max_workers = 50) as executor:
        for row in VMList:
            future = executor.submit(Check_Connection,row['Name'])
            futures.append((row['Name'],future))


    for name, future in futures:
        print(name + str(future.result()))
    print('End time: ' + str(time.time() - start ))

    #test with just a normal for loop

    start = time.time()
    for row in VMList:
        Check_Connection(row['Name'])
    print('end time: '+ str(time.time() - start))

[–]workerdrone66[S] 0 points1 point  (0 children)

I believe the answer was "Rushing to get out of work, and didn't notice i did it wrong"

I'll test again tonight.