-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutils.py
More file actions
57 lines (47 loc) · 1.56 KB
/
utils.py
File metadata and controls
57 lines (47 loc) · 1.56 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Time : 2017/12/3 21:55
# @Author : lingxiangxiang
# @File : utils.py
import codecs
import threading
import os
class WriteLog(threading.Thread):
def __init__(self, logName):
super(WriteLog, self).__init__()
self.logName = logName
self.lock = threading.Lock() #课上这里写是Lock没有写()
self.contexts = []
self.mkfile()
def mkfile(self):
if not os.path.exists(self.logName):
with codecs.open(self.logName, 'w') as f:
f.write("This file is log for {0}\n".format(self.logName))
def write(self, context):
self.contexts.append(context)
def run(self):
while 1:
self.lock.acquire()
if len(self.contexts) != 0:
with codecs.open(self.logName, "a") as f:
for context in self.contexts:
f.write(context)
del self.contexts[:]
self.lock.release()
# class A(threading.Thread):
# def __init__(self):
# super(A, self).__init__()
# self.contexts = []
# self.lock = threading.Lock()
# def run(self):
# while 1:
# self.lock.acquire()
# if len(self.contexts) !=0:
# with open('main.log', 'a') as f:
# for context in self.contexts:
# f.write(context)
# del self.contexts[:]
# self.lock.release()
#
# def write(self, context):
# self.contexts.append(context)