+关注
已关注

分类  

暂无分类

标签  

暂无标签

日期归档  

暂无数据

用于登录python的线程

发布于2021-03-04 22:27     阅读(545)     评论(0)     点赞(17)     收藏(3)


0

1

2

3

4

5

6

7

8

9

我正在尝试为程序编写记录器,该程序始终以每秒高的日志速率运行。我试图编写简单明了的记录器,但是由于它一直在访问文件,因此在其他设备上可能很慢,因此我在记录器类中创建了一个列表来缓存许多日志。我需要一个单独的线程在文件上写入数组并清空数组。但是在锁定线程的同时,它使整个程序变慢,我想不出任何解决方案。这是我的记录器课程,我需要尽快。

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




解决方案



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

0

1

2

3

4

5

6



所属网站分类: 技术文章 > 问答

作者:黑洞官方问答小能手

链接: https://www.pythonheidong.com/blog/article/878037/e8d805b8a7bf57800199/

来源: python黑洞网

任何形式的转载都请注明出处,如有侵权 一经发现 必将追究其法律责任

17 0
收藏该文
已收藏

评论内容:(最多支持255个字符)