-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgenerator.py
198 lines (156 loc) · 5.86 KB
/
generator.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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
# -*- coding: utf-8 -*-
"""
Created on 2018/4/2
@author: qiusy
@file: generate_id.py
"""
import logging
import uuid
import time
import os
ALPHABET = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789abcdefghijklmnopqrstuvwxyz'
# Tue, 21 Mar 2006 20:50:14.000 GMT
#TWEPOCH = 1142974214000L
# 2001/11/05 00:00:00 CST
TWEPOCH = 1004889600000L
log = logging.getLogger(__name__)
class SnowFlakeId(object):
def __init__(self, snowflake_id):
if isinstance(snowflake_id, int) or isinstance(snowflake_id, long):
self.snowflake_id = snowflake_id
else:
raise TypeError("SnowFlakeId must init by a integer.")
@property
def timestamp(self):
timestamp = self.snowflake_id >> 22
timestamp += TWEPOCH
return timestamp
@property
def mac_addr(self):
mac_addr = self.snowflake_id >> 12
mac_addr = mac_addr & 0x3ff
return mac_addr
@property
def pid(self):
pid = self.snowflake_id >> 8
pid = pid & 0xf
return pid
@property
def sequence(self):
sequence = self.snowflake_id & 0xff
return sequence
def base62id(self):
id_list = []
snowflake_id = self.snowflake_id
while snowflake_id > 0:
snowflake_id, rem = divmod(snowflake_id, 62)
id_list.append((ALPHABET[rem]))
if len(id_list) < 11:
id_list.append(ALPHABET[0])
id_str = "".join(reversed(id_list))
return id_str
@classmethod
def decode_base62id(cls, base62_id):
snowflake_id = 0
for x in base62_id:
snowflake_id = snowflake_id * 62 + ALPHABET.index(x)
return SnowFlakeId(snowflake_id)
def __str__(self):
return "SnowFlakeId('{0}')".format(self.snowflake_id)
class SnowFlakeGenerator(object):
sequence = 0
last_timestamp = -1
def __init__(self):
pass
def generate_id(self):
pid = os.getpid()
mac_addr = uuid.getnode()
timestamp = 0
sequence = 0
retry = 3
while retry:
timestamp = long(time.time() * 1000)
sequence = SnowFlakeGenerator.sequence
SnowFlakeGenerator.sequence = (SnowFlakeGenerator.sequence + 1) % 256
if SnowFlakeGenerator.last_timestamp > timestamp:
log.warn("clock is moving backwards. waiting until %i" % SnowFlakeGenerator.last_timestamp)
time.sleep(SnowFlakeGenerator.last_timestamp - timestamp)
retry -= 1
continue
elif SnowFlakeGenerator.last_timestamp == timestamp:
if SnowFlakeGenerator.sequence == 0:
log.warn("SnowFlakeGenerator sequence is over run.")
time.sleep(0.001)
retry -=1
continue
break
id_value = (((timestamp - TWEPOCH) << 22 ) |
((mac_addr & 0x3ff) << 12) |
((pid & 0xf) << 8) |
sequence)
# log.debug("%d, %d, %d, %d", timestamp, mac_addr, pid, sequence)
log.debug("%d, %d, %d, %d", timestamp - TWEPOCH, mac_addr & 0x3ff, pid & 0xf, sequence)
return SnowFlakeId(id_value)
if __name__ == "__main__":
# logging.basicConfig(level=logging.DEBUG)
logging.basicConfig(level=logging.INFO,
format='[%(asctime)s][%(levelname)s][%(name)s][%(process)d][%(threadName)s] %(filename)s:%(lineno)d: %(message)s',
datefmt='%m-%d %H:%M',
filename='generator.log',
filemode='a+'
)
def multi_process_generate(process_num, range_num):
for x in range(process_num):
pid = os.fork()
if pid == 0:
id_generator = SnowFlakeGenerator()
for i in xrange(range_num):
test_id = id_generator.generate_id()
log.info("{} {}".format(test_id,test_id.base62id()))
break
else:
print 'I (%s) just created a child process (%s).' % (os.getpid(), pid)
import threading
class MyThread(threading.Thread):
def __init__(self, range_num):
if not isinstance(range_num, int):
raise TypeError("range_num must be int")
self.range_num = range_num
super(MyThread, self).__init__()
def run(self):
print "running {}".format(self.name)
id_generator = SnowFlakeGenerator()
for i in xrange(self.range_num):
test_id = id_generator.generate_id()
log.info("{} {}".format(test_id,test_id.base62id()))
def multi_thread_generate(thread_num, range_num):
threads = [ MyThread(range_num) for x in range(thread_num)]
for t in threads:
t.start()
for t in threads:
t.join()
lock = threading.RLock()
class SafeMyThread(threading.Thread):
def __init__(self, range_num):
if not isinstance(range_num, int):
raise TypeError("range_num must be int")
self.range_num = range_num
super(SafeMyThread, self).__init__()
def run(self):
print "running {}".format(self.name)
id_generator = SnowFlakeGenerator()
for i in xrange(self.range_num):
if lock.acquire():
test_id = id_generator.generate_id()
lock.release()
log.info("{} {}".format(test_id,test_id.base62id()))
def safe_multi_thread_generate(thread_num, range_num):
threads = [ SafeMyThread(range_num) for x in range(thread_num)]
for t in threads:
t.start()
for t in threads:
t.join()
#multi_process_generate(8, 10000)
multi_process_generate(1, 100000)
multi_thread_generate(8, 100000)
#safe_multi_thread_generate(8, 10000)