Python Forum
Receiving this error in my "response" and causes script to return wrong status
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Receiving this error in my "response" and causes script to return wrong status
#7
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.
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.
Reply


Messages In This Thread
RE: Receiving this error in my "response" and causes script to return wrong status - by deanhystad - Aug-11-2023, 09:22 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Wrong type error rowan_bradley 6 1,349 Aug-07-2023, 10:44 AM
Last Post: rowan_bradley
  Compiles Python code with no error but giving out no output - what's wrong with it? pythonflea 6 1,654 Mar-27-2023, 07:38 AM
Last Post: buran
  Am I wrong or is Udemy wrong? String Slicing! Mavoz 3 2,692 Nov-05-2022, 11:33 AM
Last Post: Mavoz
  SMA (simple moving avg) Not receiving Data (stock prices). gdbengo 2 1,499 Jul-31-2022, 08:20 PM
Last Post: paulyan
  Receiving snmp traps with more than one Community String ilknurg 0 2,300 Jan-19-2022, 09:02 AM
Last Post: ilknurg
  return out of loops syntax error felixf 7 3,532 Nov-03-2020, 01:00 PM
Last Post: perfringo
  ERROR: Command errored out with exit status 1 calesii 3 7,250 Oct-23-2020, 05:39 PM
Last Post: snippsat
  SystemError: error return without exception set!!! faryad13 3 3,807 Oct-23-2020, 02:32 PM
Last Post: ATARI_LIVE
  Coding error- Not sure where I have put error markers against the code that is wrong Username9 1 1,779 Sep-28-2020, 07:57 AM
Last Post: buran
  Empty response to request causing .json() to error t4keheart 1 10,206 Jun-26-2020, 08:35 PM
Last Post: bowlofred

Forum Jump:

User Panel Messages

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