-
Notifications
You must be signed in to change notification settings - Fork 31
/
Copy pathexample.py
142 lines (123 loc) · 5.2 KB
/
example.py
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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
import time
from concurrent.futures import ProcessPoolExecutor, ThreadPoolExecutor
class ConcurrentTimedRotatingFileHandlerTest:
"""
ConcurrentTimedRotatingFileHandler 测试
"""
def __init__(self):
import logging
import logging.config
import concurrent_log
log_conf = {
'version': 1,
'formatters': {
'default': {
'format': '%(asctime)s - %(process)d-%(threadName)s - '
'%(pathname)s[line:%(lineno)d] - %(levelname)s: %(message)s',
'datefmt': "%Y-%m-%d %H:%M:%S"
},
},
'handlers': {
'file': {
'level': 'DEBUG',
'class': 'logging.handlers.ConcurrentTimedRotatingFileHandler',
'backupCount': 100,
'when': 's',
'delay': True,
'filename': 'log/test.log',
'encoding': 'utf-8',
'formatter': 'default',
}
},
'root': {
'handlers': ['file'],
'level': 'DEBUG',
},
}
logging.config.dictConfig(log_conf)
self.logger = logging.getLogger(__name__)
def write_log(self, index):
self.logger.debug('debug-%s' % index)
self.logger.info('info-%s' % index)
self.logger.warning('警告-%s' % index)
self.logger.error('报错-%s' % index)
self.logger.critical('严重-%s' % index)
def mutil_thread_write_log(self):
with ThreadPoolExecutor(100) as thread_pool:
for i in range(1000):
thread_pool.submit(self.write_log, i).add_done_callback(self._executor_callback)
def mutil_process_write_log(self):
with ProcessPoolExecutor() as process_pool:
for i in range(100):
process_pool.submit(self.mutil_thread_write_log).add_done_callback(self._executor_callback)
def _executor_callback(self, worker):
worker_exception = worker.exception()
if worker_exception:
print("Worker return exception: ", self.worker_exception)
class TimedRotatingFileHandlerTest:
"""
TimedRotatingFileHandler 测试
"""
def __init__(self):
import logging
import logging.config
log_conf = {
'version': 1,
'disable_existing_loggers': False,
'formatters': {
'default': {
'format': '%(asctime)s - %(process)d-%(threadName)s - '
'%(pathname)s[line:%(lineno)d] - %(levelname)s: %(message)s',
'datefmt': "%Y-%m-%d %H:%M:%S"
},
},
'handlers': {
'file': {
'level': 'DEBUG',
'class': 'logging.handlers.TimedRotatingFileHandler',
'backupCount': 100,
'when': 's',
'delay': True,
'filename': 'log2/test.log',
'encoding': 'utf-8',
'formatter': 'default',
}
},
'root': {
'handlers': ['file'],
'level': 'DEBUG',
},
}
import os
file_path = os.path.split(log_conf.get("handlers").get("file").get("filename"))[0]
if not os.path.exists(file_path):
os.makedirs(file_path)
logging.config.dictConfig(log_conf)
self.logger = logging.getLogger(__name__)
def write_log(self, index):
self.logger.debug('debug-%s' % index)
self.logger.info('info-%s' % index)
self.logger.warning('警告-%s' % index)
self.logger.error('报错-%s' % index)
self.logger.critical('严重-%s' % index)
def mutil_thread_write_log(self):
with ThreadPoolExecutor(100) as thread_pool:
for i in range(100000):
thread_pool.submit(self.write_log, i).add_done_callback(self._executor_callback)
def _executor_callback(self, worker):
worker_exception = worker.exception()
if worker_exception:
print("Worker return exception: ", self.worker_exception)
if __name__ == "__main__":
print("50W日志写入测试")
begin_time = time.time()
# 多进程写入日志,进程数与CPU核心数一致,使用文件锁实现进程并发控制,防止脏数据以及日志丢失
# 每个进程100个线程共需写入五千行日志,由于GIL原因,并发只存在一个线程,但是会存在线程上下文切换,使用线程锁防止脏数据和日志丢失
ConcurrentTimedRotatingFileHandlerTest().mutil_process_write_log()
use_time = time.time() - begin_time
print("ConcurrentTimedRotatingFileHandler 耗时:%s秒" % use_time)
begin_time = time.time()
# 每个进程100个线程共需写入所有日志,由于GIL原因,并发只存在一个线程,但是会存在线程上下文切换,同样需要锁机制防止脏数据和日志丢失
TimedRotatingFileHandlerTest().mutil_thread_write_log()
use_time = time.time() - begin_time
print("TimedRotatingFileHandler 耗时:%s秒" % use_time)