Python 使用不同的参数同时运行相同的函数

Python 使用不同的参数同时运行相同的函数,python,python-3.x,Python,Python 3.x,我一直在尝试制作一个小型python程序来监视和返回来自不同服务器的ping结果。我已经到了这样一个地步:ping序列中的每个设备变得效率低下,并且缺乏性能。我想在python上同时连续ping我的每个目标 最好的方法是什么?谢谢你的时间 def get_latency(ip_address, port): from tcp_latency import measure_latency from datetime import datetime now = datetim

我一直在尝试制作一个小型python程序来监视和返回来自不同服务器的ping结果。我已经到了这样一个地步:ping序列中的每个设备变得效率低下,并且缺乏性能。我想在python上同时连续ping我的每个目标

最好的方法是什么?谢谢你的时间

def get_latency(ip_address, port):
    from tcp_latency import measure_latency
    from datetime import datetime
    now = datetime.now()
    current_time = now.strftime("%Y-%m-%d %H:%M:%S")
    latency = str(measure_latency(host=ip_address, port=port, runs=1, timeout=1))[1:-1]
    #add to table and upload to database function()

ip_address_list = [('google.com', '80'), ('bing.com', '80')]


#Problem
#run function simultaneously but with different arguments
get_latency(ip_address_list[0][0], ip_address_list[0][1])
get_latency(ip_address_list[1][0], ip_address_list[1][1])

为此,您可以使用
for
循环。 大概是这样的:

for i in range(len(ip_address_list)):
    print(get_latency(ip_address_list[i][0], ip_address_list[i][1]))
在编写函数和返回结果之前,还应该定义模块

from tcp_latency import measure_latency
from datetime import datetime

def get_latency(ip_address, port):
    .
    .
    .
    return results

为此,您可以使用
for
循环。 大概是这样的:

for i in range(len(ip_address_list)):
    print(get_latency(ip_address_list[i][0], ip_address_list[i][1]))
在编写函数和返回结果之前,还应该定义模块

from tcp_latency import measure_latency
from datetime import datetime

def get_latency(ip_address, port):
    .
    .
    .
    return results

For循环不会同时运行

可以使用线程同时运行

见此:

import threading

def get_latency(ip_address, port):
    from tcp_latency import measure_latency
    from datetime import datetime
    now = datetime.now()
    current_time = now.strftime("%Y-%m-%d %H:%M:%S")
    latency = str(measure_latency(host=ip_address, port=port, runs=1, timeout=1))[1:-1]
    #add to table and upload to database function()

ip_address_list = [('google.com', '80'), ('bing.com', '80')]

#adding to thread

t1 = threading.Thread(target=get_latency, args=(ip_address_list[0][0], ip_address_list[0][1])) 

t2 = threading.Thread(target=get_latency, args=(ip_address_list[1][0], ip_address_list[1][1])) 

# starting thread 
t1.start() 
t2.start() 

# wait until thread 1 is completely executed 
t1.join() 
# wait until thread 2 is completely executed 
t2.join() 

# both threads completely executed 
print("Done!") 

For循环不会同时运行

可以使用线程同时运行

见此:

import threading

def get_latency(ip_address, port):
    from tcp_latency import measure_latency
    from datetime import datetime
    now = datetime.now()
    current_time = now.strftime("%Y-%m-%d %H:%M:%S")
    latency = str(measure_latency(host=ip_address, port=port, runs=1, timeout=1))[1:-1]
    #add to table and upload to database function()

ip_address_list = [('google.com', '80'), ('bing.com', '80')]

#adding to thread

t1 = threading.Thread(target=get_latency, args=(ip_address_list[0][0], ip_address_list[0][1])) 

t2 = threading.Thread(target=get_latency, args=(ip_address_list[1][0], ip_address_list[1][1])) 

# starting thread 
t1.start() 
t2.start() 

# wait until thread 1 is completely executed 
t1.join() 
# wait until thread 2 is completely executed 
t2.join() 

# both threads completely executed 
print("Done!") 

查看异步编程或为每个目标使用新线程,您需要ping。除了@AlexanderFalk注释外,您可能还需要查看多处理或线程包,比较一下它们之间的区别:我认为这对于堆栈溢出来说太广泛了。查看异步编程或为每个目标使用新线程,您希望ping。除了@AlexanderFalk comment,您可能希望查看多处理或线程包,比较一下它们之间的区别:我认为这对于堆栈溢出来说太宽了。这不会同时产生任何结果,因此忽略了问题,这将搜索一些并行解决方案。这不会同时产生任何结果,因此忽略了问题,它搜索一些并行解决方案。这与我正在寻找的类似,我们如何才能添加for循环以使代码支持可变数量的线程?可能依赖于len(ip_address_list)loop over ip_address_list这与我正在寻找的类似,我们如何才能添加for循环以使代码支持可变数量的线程?可能依赖于len(ip地址列表)在ip地址列表上循环