ellaRT
ellaRT

Reputation: 1366

Persistent List Python Implementation

I have class that sends logs to an exposed API. Now what I want to happen is to save/persists failed logs in a list so that it can be resent to the server again.

This is what I have so far. You may notice that I have declared a class variable, and I have read that this is not really advisable.

My concern is, is there a better way to persist lists or queues?

from collections import *
import time
import threading
import requests
import json


URL_POST = "http:/some/url/here"

class LogManager:
    listQueue = deque()

    def post(self, log):
        headers = {"Content-type": "application/json",
               "Accept": "text/plain", "User-Agent": "Test user agent"}
        resp = requests.post(URL_POST, data=json.dumps(log), headers=headers)
        return resp.status_code

    def send_log(self, log):
        try:
            print ("Sending log to backend")
            self.post(log)
        except: # sending to server fails for some reason
            print ("Sending logs to server fail, appending to Queue")
            LogManager.listQueue.append(log)

    def resend_log(self, log):
        print ("checking if deque has values")

        if LogManager.listQueue:
            logToPost = LogManager.listQueue.popleft()
            try:
                self.post(logToPost)
                print ("resending failed logs")
            except: #for some reason it fails again
                LogManager.listQueue.appendleft(logToPost)
                print ("appending log back to deque")

    def run(self,log):
        t1 = threading.Thread(target=self.send_log, args=(log,))
        t2 = threading.Thread(target=self.resend_log,args=(log,))
        t1.start()
        time.sleep(2)
        t2.start()
        t1.join()
        t2.join()

if __name__ == "__main__":
    while True:
        logs =  LogManager()
        logs.run({"some log": "test logs"})

Upvotes: 0

Views: 268

Answers (1)

TroyHurts
TroyHurts

Reputation: 336

Since the only variable LogManager stores is a persistent one, there doesn't seem to be any need to re-instantiate the class each time. I would probably move the logs = LogManager() line outside of the while-loop and change list_queue to an instance variable, self.list_queue that is created in the class' __init__ method. This way you'll have one LogManager with one queue that just calls its run method each loop.

That said, if you're keeping it inside the loop because your class will have further functionality that requires a new instance each loop, then using a class variable to track a list between instances is exactly what class variables are for. They're not inadvisable at all; the example given in the documentation you link to is bad because they're using a class variable for data that should be instance-specific.

Upvotes: 1

Related Questions