I'm trying to implement the client-server architecture in Python, where I have:
- Server application
- List of clients, who can subscribe to updates via API (sending POST requests to /subscribe endpoint).
It works fine. On the server side, I have a list of subscriber's URLs.
The main idea is to send requests from the server to all subscribed clients every X seconds (something similar to monitoring system).
I'm trying to do this part using threads:
class Monitor(threading.Thread):
def __init__(self):
super(Monitor, self).__init__()
self.setDaemon(True)
def send_notifications(self, subscribers):
for subscriber in subscribers:
request.post(subscriber["url"], json=subscriber["data"], timeout=0.5)
def run(self):
subscribers = get_subscribers() # getting list of subscribers via API call.
while True:
self.send_notifications(subscribers)
time.sleep(Y)
More or less it works, but I need to improve it a little. The expected behaviour is: The server should send notifications every X seconds to each subscribed client. If sending the notification to some client fails for Y minutes(5 minutes for example) it should unsubscribe this unresponsive client.
Is there some best practices for this?