HiveBrain v1.2.0
Get Started
← Back to all entries
patternpythonMinor

Polling multiple servers

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
multiplepollingservers

Problem

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!

Solution

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))

Code Snippets

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

Context

StackExchange Code Review Q#30129, answer score: 2

Revisions (0)

No revisions yet.