# Polling multiple servers

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 time

servers = ['1.1.1.1', '1.1.1.2']

"""
Auxiliary class used to provide arguments to threads
"""

def __init__(self, target, *args):
self.target = target
self.args = args

def run (self):
self.target(*self.args)

class ServerResults(object):
def __init__(self):
self.results_list = []

self.results_list.append(some_argument)

def poll_server(server, results):
response = requests.get(server, timeout=10)

def process_results(results):
# Do something with the results

def main():
while True:
results = ServerResults()
for s in servers:
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!

• Is the while statement supposed to be in the process_results function? Aug 24, 2013 at 14:57
• Thank you Aseem. No, the while statement is not in the process_results() function. I have added a main() function to clarify this. Aug 25, 2013 at 6:57

The two classes seem useless to me.

ServerResults only contains a list, so just use a list. Edit: see comment below.

The CallThreads class is unnecessary, this:

t = CallThreads(poll_server, s, results)


can be written like that:

t = Thread(target=poll_server, args=(s, results))


Note that you could also use the partial function:

t = Thread(target=partial(poll_server, s, results))


or a lambda:

t = Thread(target=lambda: poll_server(s, results))

• Thank you. ServerResults is actually much more than a list, I just show a list in this example code for simplicity's sake. I am interested in this partial function, where is this documented? Googling for python target=partial turns up some blog posts but no real documentation. Thanks! Sep 2, 2013 at 9:36
• Oops, I was going to put a link to the documentation but forgot. It's in the standard library: functools.partial. target is a keyword argument of Thread(), you simply had to search for python partial. Sep 2, 2013 at 9:45
• Great, thanks. I had to add from functools import partial. I'm still playing with it, thank you. Sep 2, 2013 at 9:56
• The code which uses target=partial is segfaulting, please see the SO question. Sep 2, 2013 at 17:55
• Sorry, I don't know much about debugging segfaults. Have you tried using gdb ? Sep 3, 2013 at 12:11