Aug-11-2023, 09:22 PM
(This post was last modified: Aug-11-2023, 09:22 PM by deanhystad.)
The thing returned by "executor.map(ping, park)" is a lazy iterator. executor.map() returns immediately, but if you want the values, it will make you wait until they are available. You can do this as you did in your last post, by iterating through the results. Or you could use something like list() to stuff them all in a list.
I am glad it is faster. Try increasing the number of threads. Results will eventually plateau.
I don't understand the writing problem. The 0, 1 is available right there in the result. Maybe you will find this helpful.
I am glad it is faster. Try increasing the number of threads. Results will eventually plateau.
I don't understand the writing problem. The 0, 1 is available right there in the result. Maybe you will find this helpful.
import subprocess from concurrent.futures import ThreadPoolExecutor ips = ["10.0.0.5", "127.0.0.1", "::1"] def ping(ip): return ( ip, subprocess.run( f"ping {ip} -n 1", stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL ).returncode, ) executor = ThreadPoolExecutor(4) for result in executor.map(ping, ips): print(result[0], "is down" if result[1] else "is up")\
Output:10.0.0.5 is down
127.0.0.1 is up
::1 is up
Or you could stuff the results into a dataframe.import subprocess from concurrent.futures import ThreadPoolExecutor import pandas as pd ips = ["10.0.0.5", "127.0.0.1", "::1"] def ping(ip): return ( ip, subprocess.run( f"ping {ip} -n 1", stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL ).returncode, ) executor = ThreadPoolExecutor(4) df = pd.DataFrame(executor.map(ping, ips), columns=["address", "state"]) print(df)
Output: address state
0 10.0.0.5 1
1 127.0.0.1 0
2 ::1 0
And pandas has a function for writing the dataframe as an excel format file.