-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathregistry.py
139 lines (124 loc) · 4.15 KB
/
registry.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
from abc import ABCMeta, abstractmethod
from autotx.error.errors import IllegalError, EmptyError
from autotx.module.mid import SplitMid
from autotx.module.moduletype import LegalLetterType
from autotx.module.moduletype import CheckType
from autotx.module.moduletype import LegalType
from autotx.utils.balance import Select
from autotx.utils.rwlock import RWLock
class Registry(metaclass=ABCMeta):
# 注册模块
@abstractmethod
def Register(self, module):
pass
# 注销模块
@abstractmethod
def UnRegister(self, mid):
pass
# 获取一个指定类型的实例(根据处理时间快慢评分)
@abstractmethod
def Get(self, mType):
pass
# 获取指定类型的所有实例
@abstractmethod
def GetAllByType(self, moduleType):
pass
# 获取所有实例
@abstractmethod
def GetAll(self):
pass
# 清除所有注册实例
@abstractmethod
def Clear(self):
pass
class Registrar(Registry):
def __init__(self):
self.__moduleTypeDict = {}
self.__rwLock = RWLock()
def Register(self, module):
if module is None:
return False, IllegalError('module is none')
mid = module.ID()
separateMID, err = SplitMid(mid)
if err is not None:
return False, IllegalError('invalid mid: {mid}'.format(mid=mid))
mType = LegalLetterType[list(separateMID)[0]]
if CheckType(mType, module) is False:
return False, IllegalError(
'invalid module type: {mType}'.format(mType=mType))
# 加写锁
self.__rwLock.acquire_write()
try:
modules = self.__moduleTypeDict.get(mType)
if modules is None:
modules = {}
if modules.get(mid) is not None:
return False, None
modules[mid] = module
self.__moduleTypeDict[mType] = modules
return True, None
finally:
self.__rwLock.release()
def UnRegister(self, mid):
if mid is None or str(mid) == '':
return False, IllegalError('empty mid')
separateMID, err = SplitMid(mid)
if err is not None:
return False, IllegalError('invalid mid: {mid}'.format(mid=mid))
mType = LegalLetterType[list(separateMID)[0]]
# 加读锁
self.__rwLock.acquire_read()
try:
modules = self.__moduleTypeDict[mType]
if modules is not None:
if modules[mid] is not None:
del modules[mid]
return True, None
finally:
self.__rwLock.release()
def Get(self, moduleType):
modules, err = self.GetAllByType(moduleType)
if err is not None:
return None, err
costs = {}
for mid, module in modules.items():
costs[mid] = module.CalculateCost()
costArr = costs.items()
index = Select(list(costArr)) # 筛选出更优的实例
selectModuleID = list(costArr)[index][0]
return modules[selectModuleID], None
def GetAllByType(self, mType):
if LegalType(mType) is False:
return None, IllegalError(
'invalid module type: {mType}'.format(mType=mType))
# 加读锁
self.__rwLock.acquire_read()
try:
modules = self.__moduleTypeDict[mType]
if len(modules) == 0:
return None, EmptyError()
result = {}
for mid, module in modules.items():
result[mid] = module
return result, None
finally:
self.__rwLock.release()
def GetAll(self):
result = {}
# 加读锁
self.__rwLock.acquire_read()
try:
for i, modules in self.__moduleTypeDict:
for mid, module in modules:
result[mid] = module
return result
finally:
self.__rwLock.release()
def Clear(self):
# 加读锁
self.__rwLock.acquire_read()
try:
del self.__moduleTypeDict
self.__moduleTypeDict = {}
finally:
self.__rwLock.release()