python雪花_python实现的雪花算法一枚
发布日期:2021-09-12 14:09:24 浏览次数:1 分类:技术文章

本文共 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 如侵犯您的版权,请留言回复原文章的地址,我们会给您删除此文章,给您带来不便请您谅解!

上一篇:python中给csv替换数据_Python把csv数据写入list和字典类型的变量脚本方法
下一篇:python sklearn_【Python】使用Python进行主成分分析,,使用sklearn库

发表评论

最新留言

逛到本站,mark一下
[***.202.152.39]2024年04月19日 06时19分52秒

关于作者

    喝酒易醉,品茶养心,人生如梦,品茶悟道,何以解忧?唯有杜康!
-- 愿君每日到此一游!

推荐文章