凯哥带你用python撸算法之雪花算法
2024-08-30 10:43:48
import time
class Snow(object):
def __init__(self, idx=None):
init_date = time.strptime('2010-01-01 00:00:00', "%Y-%m-%d %H:%M:%S")
self.start = int(time.mktime(init_date))
self.last = int(time.time())
self.count_id = 0
self.idx = idx if idx else 0
def get(self):
now = int(time.time())
temp = now - self.start
if len(str(temp)) < 9:
length = len(str(temp))
s = '0' * (9 - length)
temp = s + str(temp)
if now == self.last:
self.count_id += 1
else:
self.count_id = 0
self.last = now
if len(str(self.idx)) < 2:
length = len(str(self.idx))
s = '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 = '0' * (5 - length)
count_id_data = s + count_id_data
return ''.join([temp, self.idx, count_id_data])
if __name__ == '__main__':
import threading
snow = Snow('001')
def echo():
print(snow.get())
threads = [threading.Thread(target=echo) for i in range(100)]
for t in threads:
t.start()
for t in threads:
t.join()
最新文章
- 7.11 数据注解特性--InverseProperty
- [Java Web]Error parsing HTTP request header Note: further occurrences of HTTP header parsing errors
- 【bzoj1500】 noi2005—维护数列
- SharePoint固定的Footer
- Android适配器之ArrayAdapter、SimpleAdapter和BaseAdapter的简单用法与有用代码片段(转)
- 在Tomcat里使用配置连接池连接数据库
- C#各类型大小
- Kettle ETL 来进行mysql 数据同步——试验环境搭建(表中无索引,无约束,无外键连接的情况)
- WordPress &#39;is_serialized()&#39;远程任意代码执行漏洞(CVE-2013-4338)
- PHP数据学习-二维数组【3】
- html 商品展示框
- Java中实例方法,实例变量,静态方法,静态变量,final方法重写的问题,覆盖
- POJ 2007 Scrambled Polygon 凸包
- Ajax调用asp.net后台代码
- Spring Security入门(3-6)Spring Security 的鉴权 - 自定义权限前缀
- Android 插件化技术窥探
- C语言随机数使用方法
- 2016年,总结篇 续 如何从 JQ 转到 VueJS 开发(一)
- container
- python中pip升级