'update server once a second and not more - in python

I have a client code that sniffes network traffic and saves in a dictionary - all the active IP adresses that send packets. When a new IP adress sends packet - I save it in the dictionary and send the updated dictionary to a HTTP server. The problem - I don't want to send update more than once in a second. and sometimes there many new IP addresses within miliseconds, so I can't send after every update. any way to schedule it in python so that he will sendonce a second or less? thank you



Solution 1:[1]

Yes, this can be achieved by appending the new entries in a list and sending the whole list after each second.

The list can be global and the function can be like this. You can pass this function in a separate thead

import time
def sendafterasecond():
   while True:
      if len(data_list) > 0:
         send(data_list)
         data_list = [] # empty it again
      time.sleep(1)

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Ali Zahid Raja