I have an application which polls a bunch of servers every few minutes. To do this, it spawns one thread per server to poll (15 servers) and writes back the data to an object:
import requests
import threading
import time
servers = ['1.1.1.1', '1.1.1.2']
class CallThreads(threading.Thread):
"""
Auxiliary class used to provide arguments to threads
"""
def __init__(self, target, *args):
self.target = target
self.args = args
threading.Thread.__init__(self)
def run (self):
self.target(*self.args)
class ServerResults(object):
def __init__(self):
self.results_list = []
def add_server(some_argument):
self.results_list.append(some_argument)
def poll_server(server, results):
response = requests.get(server, timeout=10)
results.add_server(response.status_code);
def process_results(results):
# Do something with the results
def main():
while True:
results = ServerResults()
for s in servers:
t = CallThreads(poll_server, s, results)
t.daemon = True
t.start()
time.sleep(300)
process_results(results.results_list)
if __name__ == '__main__':
main()
This is my first non-trivial Python application, so I would appreciate any critique, comments, or suggestions. Thank you!
process_results
function? \$\endgroup\$ – Aseem Bansal Aug 24 '13 at 14:57while
statement is not in theprocess_results()
function. I have added amain()
function to clarify this. \$\endgroup\$ – dotancohen Aug 25 '13 at 6:57