-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils.py
82 lines (61 loc) · 1.52 KB
/
utils.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
#!/usr/bin/env python
# encoding: utf-8
"""
utils.py
Created by jack ju on 2015-03-16.
Copyright (c) 2015 TIKY. All rights reserved.
"""
import threading
import time
class Status(object):
"""
状态机,封装起来为了保证多线程安全
"""
INITINAL = 0 # 初始状态
CHECKING = 1 # Check 密码的阶段
INSERVICES = 2 # 服务进行中
def __init__(self):
self.__locker = threading.Lock()
self.__value = Status.INITINAL
def set(self, value):
self.__locker.acquire()
try:
self.__value = value
finally:
self.__locker.release()
@property
def value(self):
self.__locker.acquire()
try:
value = self.__value
finally:
self.__locker.release()
return value
class Timing(object):
"""
计时类,用来计算提供服务的时间
"""
def __init__(self):
self.__start = None
def start(self):
self.__start = time.time()
def end(self):
now = time.time()
return int(self.__start), int(now - self.__start)
if __name__ == '__main__':
# status = Status()
# print status.value
# status.set(Status.CHECKING)
# print status.value
# status.set(Status.INSERVICES)
# print status.value
timing = Timing()
timing.start()
time.sleep(1)
print timing.end()
timing.start()
time.sleep(2)
print timing.end()
timing.start()
time.sleep(3)
print timing.end()