3

I have been attempting to make a small python program to monitor and return ping results from different servers. I have reached a point where pinging each device in the sequence has become inefficient and lacks performance. I want to continuously ping each one of my targets at the same time on my python.

What would the best approach to this be? Thanks for your time

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])
Unloq Frit
  • 45
  • 3
  • Look in to asynchronous programming or use a new thread for each target, you want to ping. – Alexander Falk Jan 18 '20 at 14:08
  • In addition to @AlexanderFalk comment, you may want to have a look at multiprocessing or threading packages, compare the following for the difference between them: https://stackoverflow.com/questions/3044580/multiprocessing-vs-threading-python – Nico Albers Jan 18 '20 at 14:30
  • I think this is too broad for Stack Overflow. – AMC Jan 18 '20 at 17:34

2 Answers2

2

For loop does not run in simultaneous.

You can use threading to run in simultaneous.

see this:

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!") 
tard
  • 107
  • 6
  • This is similar to what I am looking for, how would we be able to add a for loop to make the code support a variable amount of threads? Probably relying on len(ip_address_list) – Unloq Frit Jan 18 '20 at 14:43
  • loop over ip_address_list – tard Jan 18 '20 at 14:52
0

You can use a for loop for this purpose. Something like this:

for i in range(len(ip_address_list)):
    print(get_latency(ip_address_list[i][0], ip_address_list[i][1]))

Also you should define the modules before writing the function and return the results

from tcp_latency import measure_latency
from datetime import datetime

def get_latency(ip_address, port):
    .
    .
    .
    return results
codeblaze
  • 73
  • 8
  • This won't make anything simultaneously and therefore misses the question, which searches for some parallel solution. – Nico Albers Jan 18 '20 at 14:29