从多线程函数中获取 return
get return from multi-threaded functions
我正在制作一个 IP 扫描器,它适用于多线程。
在 运行 宁线程之后,我打印返回值(列表)
但是函数再次 运行ning 没有返回列表。
当函数等于一个变量时发生。
这需要更多时间,因为函数 运行 一个接一个。
怎么办?
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)
你运行同样的功能两次。第一次在专用线程,然后又在主线程。
使用executors and futures来避免:
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())
我正在制作一个 IP 扫描器,它适用于多线程。
在 运行 宁线程之后,我打印返回值(列表)
但是函数再次 运行ning 没有返回列表。
当函数等于一个变量时发生。 这需要更多时间,因为函数 运行 一个接一个。
怎么办?
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)
你运行同样的功能两次。第一次在专用线程,然后又在主线程。
使用executors and futures来避免:
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())