首页 > 解决方案 > 用于登录python的线程

问题描述

我正在尝试为一个程序编写一个记录器,该程序每秒的日志记录率一直很高。我尝试编写不带踏板的直接记录器,但因为它不断访问文件,在其他设备上可能会很慢,所以我在记录器类中创建了一个列表来缓存许多日志。我需要一个单独的线程来将数组写入文件并清空数组。但是在锁定线程时它会减慢整个程序的速度,我想不出任何解决方案。这是我的记录器类,我需要它尽可能快。

import threading

class Logger:
 def __init__(self):
        # declaring the class variables
        self._logpath = 'log.csv'
        self.threadlock = threading.Lock()
        self._cached = []

    def write_cache(self):
        time.sleep(__SAVE_DELAY__)
        self.threadlock.acquire()
        self._threadinglock=.acquire()
        for index in range(len(self._cached)):
            cached_log = self._cached[index]
            with open(self._systemlog, 'a+', newline='') as csvfile:
                writer = csv.writer(csvfile)
                writer.writerow([cached_log[1],  # date time
                                     cached_log[2],  # module
                                     cached_log[3],  # method
                                     cached_log[4]])  # massage
        self._cache=[]
        self._threadinglock=.release()

    def system(self, module, method, massage, printlog=False):
        datime = '{:%Y-%b-%d %H:%M:%S}'.format(datetime.datetime.now())
        if printlog:
            print(ColorText(datime + " " + module + "  " + method + " " + str(massage), 'yellow'))
   
        # writing in cache to write on file
        self._threadinglock=.acquire()
        self._cached.append(('system', datime, module, method, massage))
        self._threadinglock=.release()



标签: pythonmultithreadingloggingoptimization

解决方案



class Logger:
 def __init__(self):
        # declaring the class variables
        self._logpath = 'log.csv'
        self.threadlock = threading.Lock()
        self._cached = []
        self.stop_save = False
        self.thread = threading.Thread(target=self.save_thread, args=(lambda:self.stop_save,))
        self.thread.start()

    def __del__(self):
        self.stop_save = True
        self.thread.join()
        self.write_cache()    

    def save_thread(self, stop):
        while True:
            time.sleep(__SAVE_DELAY__)
            if stop():
                break
            self.threadlock.acquire()
            self.write_cache()
            self.threadlock.release()

    def write_cache(self):
        with open(self._systemlog, 'a+', newline='') as file:
            for index in range(len(self._system_cached)):
                cached_log = self._cached[index]
                
                writer = csv.writer(csvfile)
                writer.writerow([cached_log[0],  # date time
                                     cached_log[1],  # module
                                     cached_log[2],  # method
                                     cached_log[3]])  # massage
        self._cache=[]
        

    def system(self, module, method, massage, printlog=False):
        datime = '{:%Y-%b-%d %H:%M:%S}'.format(datetime.datetime.now())
        if printlog:
            print(ColorText(datime + " " + module + "  " + method + " " + str(massage), 'yellow'))
   
        # writing in cache to write on file
        self._threadinglock=.acquire()
        self._cached.append(('system', datime, module, method, massage))
        self._threadinglock=.release()



log = Logger()
for i in range(10**5):
    log.system("test", "Test", "Logging really fast")


推荐阅读