Python 从多线程函数获取返回

Python 从多线程函数获取返回,python,python-multithreading,python-3.8,Python,Python Multithreading,Python 3.8,我在做一个IP扫描器,可以使用多线程 运行线程后,我打印返回的值(列表) 但是这些函数在没有返回列表的情况下再次运行 当函数等于一个变量时,就会发生这种情况。 这需要更多的时间,因为函数一个接一个地运行 怎么办 def first_50IP() : prefix = "192.168.1." condition = "Destination host unreachable" condition2 = "Requ

我在做一个IP扫描器,可以使用多线程

运行线程后,我打印返回的值(列表)

但是这些函数在没有返回列表的情况下再次运行

当函数等于一个变量时,就会发生这种情况。 这需要更多的时间,因为函数一个接一个地运行

怎么办

def first_50IP() :
    
    prefix = "192.168.1."
    condition = "Destination host unreachable"
    condition2 = "Request timed out"
    list1 = []

    for xxx in range(0,50) :  

        ip = prefix + str(xxx)
        code = "ping " + ip + " -n 1 -l 1"    
        ping = os.popen(code).read()
        #ping = subprocess.check_output(code).decode('utf-8')
    
        if condition not in ping and condition2 not in ping:
            print(G + ip)  
            list1.append(ip)
    return list1
                     
def second_50IP() :

    prefix = "192.168.1."
    condition = "Destination host unreachable"
    condition2 = "Request timed out"
    list2 = []

    for xxx in range(50,100) :
      
        ip = prefix + str(xxx)
        code = "ping " + ip + " -n 1 -l 1"    
        ping = os.popen(code).read()
        #ping = subprocess.check_output(code).decode('utf-8')
    
        if condition not in ping and condition2 not in ping:
            print(G + ip)
            list2.append(ip)
    return list2

thread1 = threading.Thread(target=first_50IP) 
thread2 = threading.Thread(target=second_50IP) 

thread1.start() 
thread2.start() 
thread1.join()  
thread2.join()  

print("\nResults : \n")
final_ip1 = first_50IP()
final_ip2 = second_50IP()
print(final_ip1)
print(final_ip2)

您正在运行相同的函数两次。第一次在专用线程中,然后再次在主线程中

使用此选项可避免:

with concurrent.futures.ThreadPoolExecutor(max_workers=5) as executor:
   first = executor.submit(first_50IP)
   second = executor.submit(second_50IP)

print("\nResults : \n")
final_ip1 = first.result()
final_ip2 = second.result()
print(final_ip1)
print(final_ip2)
旁注:您可以只使用一个函数,而不是像这样使用两个几乎相同的函数:

def ping_many(start, count) :
    
    prefix = "192.168.1."
    condition = "Destination host unreachable"
    condition2 = "Request timed out"
    list1 = []

    for xxx in range(start, start+count) :  

        ip = prefix + str(xxx)
        code = "ping " + ip + " -n 1 -l 1"    
        ping = os.popen(code).read()
        #ping = subprocess.check_output(code).decode('utf-8')
    
        if condition not in ping and condition2 not in ping:
            print(G + ip)  
            list1.append(ip)
    return list1
tasks = {}
with concurrent.futures.ThreadPoolExecutor(max_workers=5) as executor:
   for start in [0, 50]:
      tasks.add(executor.submit(ping_many, start, 50))
   
print("\nResults : \n")
for task in tasks:
   print(task.result())
在这种情况下,您可以向executor提交具有不同参数的函数,如下所示:

def ping_many(start, count) :
    
    prefix = "192.168.1."
    condition = "Destination host unreachable"
    condition2 = "Request timed out"
    list1 = []

    for xxx in range(start, start+count) :  

        ip = prefix + str(xxx)
        code = "ping " + ip + " -n 1 -l 1"    
        ping = os.popen(code).read()
        #ping = subprocess.check_output(code).decode('utf-8')
    
        if condition not in ping and condition2 not in ping:
            print(G + ip)  
            list1.append(ip)
    return list1
tasks = {}
with concurrent.futures.ThreadPoolExecutor(max_workers=5) as executor:
   for start in [0, 50]:
      tasks.add(executor.submit(ping_many, start, 50))
   
print("\nResults : \n")
for task in tasks:
   print(task.result())

tasks.add(executor.submit(ping\u many,start,50))中有一个错误。
dict的实例没有“add”成员如何将其添加到完整的ip范围?192.168.1.0-192.168.1.255找到方法谢谢