本文共 1342 字,大约阅读时间需要 4 分钟。
数据量写入很大的场景使用
优点: 提高写入效率
# coding: utf-8
import time
class Snow(object):
def __init__(self, idx=None):
init_date = time.strptime('2019-01-01 00:00:00', "%Y-%m-%d %H:%M:%S")
self._init_time = int(time.mktime(init_date))
self._last_time = int(time.time())
self._count_id = 0
self._idx = idx if idx else 0
def get(self):
now = int(time.time())
temp = now - self._init_time
if len(str(temp)) < 9:
length = len(str(temp))
s = str(0) * (9 - length)
temp = s + str(temp)
if now == self._last_time:
self._count_id += 1
else:
self._count_id = 0
self._last_time = now
if len(str(self._idx)) < 2:
length = len(str(self._idx))
s = str(0) * (2 - length)
self._idx = s + str(self._idx)
if self._count_id == 99999:
time.sleep(1)
count_id_data = str(self._count_id)
if len(count_id_data) < 5:
length = len(count_id_data)
s = str(0) * (5 - length)
count_id_data = s + count_id_data
return ''.join([str(temp), self._idx, count_id_data])
if __name__ == '__main__':
import logging
import threading
logging.basicConfig(
level=logging.INFO,
datefmt='%m-%d %H:%M:%S',
format='[%(asctime)s][%(process)s][%(thread)s][%(name)s][%(levelname)s]: %(message)s'
)
logger = logging.getLogger('snow')
snow = Snow('001')
def echo():
logger.info(snow.get())
threads = [threading.Thread(target=echo) for i in range(10000)]
[t.start() for t in threads]
[t.join() for t in threads]
转载地址:https://blog.csdn.net/weixin_39545329/article/details/110325185 如侵犯您的版权,请留言回复原文章的地址,我们会给您删除此文章,给您带来不便请您谅解!