CheeseSignal 1.2.1__py2.py3-none-any.whl → 2.0.0__py2.py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Potentially problematic release.
This version of CheeseSignal might be problematic. Click here for more details.
- CheeseSignal/signal.py +333 -281
- cheesesignal-2.0.0.dist-info/METADATA +82 -0
- cheesesignal-2.0.0.dist-info/RECORD +6 -0
- {cheesesignal-1.2.1.dist-info → cheesesignal-2.0.0.dist-info}/WHEEL +1 -1
- cheesesignal-1.2.1.dist-info/METADATA +0 -273
- cheesesignal-1.2.1.dist-info/RECORD +0 -6
- {cheesesignal-1.2.1.dist-info → cheesesignal-2.0.0.dist-info}/licenses/LICENSE +0 -0
CheeseSignal/signal.py
CHANGED
|
@@ -1,362 +1,414 @@
|
|
|
1
|
-
import asyncio
|
|
2
|
-
from typing import
|
|
3
|
-
|
|
4
|
-
if TYPE_CHECKING:
|
|
5
|
-
class Signal:
|
|
6
|
-
...
|
|
1
|
+
import uuid, concurrent.futures, asyncio
|
|
2
|
+
from typing import Callable, Iterable, overload, Literal
|
|
3
|
+
from collections import OrderedDict
|
|
7
4
|
|
|
8
5
|
class Receiver:
|
|
9
|
-
|
|
6
|
+
__slots__ = ('_key', 'fn', 'runType', '_receiveNum_expected', 'autoRemove', '_receiveNum')
|
|
7
|
+
|
|
8
|
+
def __init__(self, fn: Callable, key: str | None = None, *, runType: Literal['SEQUENTIAL', 'PARALLEL', 'NO_BLOCK'] = 'SEQUENTIAL', receiveNum_expected: int = 0, autoRemove: bool = False):
|
|
10
9
|
'''
|
|
11
10
|
- Args
|
|
12
|
-
-
|
|
13
|
-
|
|
14
|
-
-
|
|
11
|
+
- fn: 接收函数
|
|
12
|
+
- key: 若不设置则自动生成一个uuid格式的字符串
|
|
13
|
+
- runType: 运行方式
|
|
14
|
+
- SEQUENTIAL: 顺序执行,等待函数执行完成后再执行下一个函数
|
|
15
|
+
- PARALLEL: 并行执行,等待所有函数执行完成后再继续
|
|
16
|
+
- NO_BLOCK: 非阻塞执行,函数在后台执行,不等待函数执行完成
|
|
17
|
+
- receiveNum_expected: 期望接收总数
|
|
18
|
+
- autoRemove: 是否在达到期望接收总数后自动移除接收器
|
|
15
19
|
'''
|
|
16
20
|
|
|
17
|
-
self.
|
|
21
|
+
self._key: str = key or str(uuid.uuid4())
|
|
18
22
|
self.fn: Callable = fn
|
|
19
|
-
self.
|
|
20
|
-
self._auto_remove: bool = auto_remove
|
|
21
|
-
self._total_receive_num: int = 0
|
|
22
|
-
self._active: bool = True
|
|
23
|
-
self._runType: Literal['ORDERED', 'CONCURRENT', 'NO_WAIT'] = runType
|
|
24
|
-
|
|
25
|
-
def reset(self):
|
|
26
|
-
'''
|
|
27
|
-
重统计置数据。
|
|
28
|
-
|
|
29
|
-
在有期望信号接受次数的情况下,`auto_remove is False`的接收器会重新开始计数并接收信号。
|
|
30
|
-
'''
|
|
31
|
-
|
|
32
|
-
self._total_receive_num = 0
|
|
33
|
-
|
|
34
|
-
@property
|
|
35
|
-
def expected_receive_num(self) -> int:
|
|
23
|
+
self.runType: Literal['SEQUENTIAL', 'PARALLEL', 'NO_BLOCK'] = runType
|
|
36
24
|
'''
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
25
|
+
运行方式
|
|
26
|
+
- SEQUENTIAL: 顺序执行,等待函数执行完成后再执行下一个函数
|
|
27
|
+
- PARALLEL: 并行执行,等待所有函数执行完成后再继续
|
|
28
|
+
- NO_BLOCK: 非阻塞执行,函数在后台执行,不等待函数执行完成
|
|
40
29
|
'''
|
|
30
|
+
self._receiveNum_expected: int = receiveNum_expected
|
|
31
|
+
''' 期望接收总数 '''
|
|
32
|
+
self.autoRemove: bool = autoRemove
|
|
33
|
+
''' 是否在达到期望接收总数后自动移除接收器 '''
|
|
41
34
|
|
|
42
|
-
|
|
35
|
+
self._receiveNum: int = 0
|
|
36
|
+
''' 接收总数 '''
|
|
43
37
|
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
self._expected_receive_num = value
|
|
38
|
+
def reset(self):
|
|
39
|
+
''' 重置统计数据 '''
|
|
47
40
|
|
|
48
|
-
|
|
49
|
-
self._signal.receivers.remove(self)
|
|
41
|
+
self._receiveNum = 0
|
|
50
42
|
|
|
51
43
|
@property
|
|
52
|
-
def
|
|
53
|
-
|
|
54
|
-
是否自动删除响应次数超出期望次数的接收器。
|
|
55
|
-
|
|
56
|
-
设置为`True`时若该receiver过期,则会立刻删除。
|
|
57
|
-
'''
|
|
58
|
-
|
|
59
|
-
return self._auto_remove
|
|
60
|
-
|
|
61
|
-
@auto_remove.setter
|
|
62
|
-
def auto_remove(self, value: bool):
|
|
63
|
-
self._auto_remove = value
|
|
64
|
-
|
|
65
|
-
if self._auto_remove and self.is_expired:
|
|
66
|
-
self._signal.receivers.remove(self)
|
|
44
|
+
def key(self) -> str:
|
|
45
|
+
return self._key
|
|
67
46
|
|
|
68
47
|
@property
|
|
69
|
-
def
|
|
70
|
-
'''
|
|
71
|
-
是否激活;未激活将忽略信号。
|
|
72
|
-
'''
|
|
73
|
-
|
|
74
|
-
return self._active
|
|
48
|
+
def receiveNum(self) -> int:
|
|
49
|
+
''' 接收总数 '''
|
|
75
50
|
|
|
76
|
-
|
|
77
|
-
def active(self, value: bool):
|
|
78
|
-
self._active = value
|
|
51
|
+
return self._receiveNum
|
|
79
52
|
|
|
80
53
|
@property
|
|
81
|
-
def
|
|
82
|
-
'''
|
|
83
|
-
【只读】 总计信号接受次数。
|
|
84
|
-
'''
|
|
54
|
+
def receiveNum_expected(self) -> int:
|
|
55
|
+
''' 期望接收总数 '''
|
|
85
56
|
|
|
86
|
-
return self.
|
|
57
|
+
return self._receiveNum_expected
|
|
87
58
|
|
|
88
59
|
@property
|
|
89
|
-
def
|
|
90
|
-
'''
|
|
91
|
-
【只读】 剩余的期望信号接受次数;返回为-1代表无期望信号接受次数。
|
|
92
|
-
'''
|
|
60
|
+
def receiveNum_remaining(self) -> int | None:
|
|
61
|
+
''' 剩余接收总数 '''
|
|
93
62
|
|
|
94
|
-
|
|
95
|
-
return -1
|
|
96
|
-
return self.expected_receive_num - self.total_receive_num
|
|
63
|
+
return self._receiveNum_expected - self._receiveNum if self._receiveNum_expected > 0 else None
|
|
97
64
|
|
|
98
65
|
@property
|
|
99
|
-
def
|
|
100
|
-
'''
|
|
101
|
-
【只读】 是否过期。
|
|
102
|
-
'''
|
|
103
|
-
|
|
104
|
-
return not self.is_unexpired
|
|
66
|
+
def is_active(self) -> bool:
|
|
67
|
+
''' 是否处于激活状态 '''
|
|
105
68
|
|
|
106
|
-
|
|
107
|
-
def is_unexpired(self) -> bool:
|
|
108
|
-
'''
|
|
109
|
-
【只读】 是否未过期。
|
|
110
|
-
'''
|
|
69
|
+
return self._receiveNum_expected == 0 or self.receiveNum_remaining > 0
|
|
111
70
|
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
return True if self.remaining_receive_num else False
|
|
71
|
+
class Signal:
|
|
72
|
+
__slots__ = ('receivers', '_sendNum')
|
|
115
73
|
|
|
116
|
-
|
|
117
|
-
def runType(self) -> Literal['ORDERED', 'CONCURRENT', 'NO_WAIT']:
|
|
74
|
+
def __init__(self):
|
|
118
75
|
'''
|
|
119
|
-
|
|
76
|
+
- Examples
|
|
77
|
+
```python
|
|
78
|
+
""" 基础用法 """
|
|
79
|
+
from CheeseSignal import Signal
|
|
120
80
|
|
|
121
|
-
|
|
81
|
+
signal = Signal()
|
|
122
82
|
|
|
123
|
-
|
|
83
|
+
def handle_1():
|
|
84
|
+
print('Handler 1 executed')
|
|
85
|
+
signal.connect(handle_1)
|
|
124
86
|
|
|
125
|
-
|
|
126
|
-
|
|
87
|
+
@signal.connect()
|
|
88
|
+
def handle_2():
|
|
89
|
+
print('Handler 2 executed')
|
|
127
90
|
|
|
128
|
-
|
|
91
|
+
if __name__ == '__main__':
|
|
92
|
+
signal.send()
|
|
129
93
|
|
|
130
|
-
@runType.setter
|
|
131
|
-
def runType(self, value: Literal['ORDERED', 'CONCURRENT', 'NO_WAIT']):
|
|
132
|
-
self._runType = value
|
|
133
94
|
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
self._receivers: List[Receiver] = []
|
|
137
|
-
self._total_send_num: int = 0
|
|
95
|
+
""" 异步用法 """
|
|
96
|
+
import asyncio
|
|
138
97
|
|
|
139
|
-
|
|
140
|
-
def connect(self, fn: Callable, *, expected_receive_num: int = 0, auto_remove: bool = False, runType: Literal['ORDERED', 'CONCURRENT', 'NO_WAIT'] = 'ORDERED'):
|
|
141
|
-
'''
|
|
142
|
-
通过函数注册响应函数。
|
|
98
|
+
from CheeseSignal import Signal
|
|
143
99
|
|
|
144
|
-
|
|
145
|
-
>>>
|
|
146
|
-
>>> def receiver(*args, **kwargs):
|
|
147
|
-
... ...
|
|
148
|
-
>>>
|
|
149
|
-
>>> signal = Signal()
|
|
150
|
-
>>> signal.connect(receiver)
|
|
100
|
+
signal = Signal()
|
|
151
101
|
|
|
152
|
-
|
|
153
|
-
|
|
102
|
+
async def handle_1():
|
|
103
|
+
print('Handler 1 executed')
|
|
104
|
+
signal.connect(handle_1)
|
|
154
105
|
|
|
155
|
-
|
|
156
|
-
|
|
106
|
+
@signal.connect()
|
|
107
|
+
async def handle_2():
|
|
108
|
+
print('Handler 2 executed')
|
|
157
109
|
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
'''
|
|
161
|
-
通过装饰器注册响应函数。
|
|
110
|
+
if __name__ == '__main__':
|
|
111
|
+
asyncio.run(signal.async_send())
|
|
162
112
|
|
|
163
|
-
>>> from CheeseSignal import Signal
|
|
164
|
-
>>>
|
|
165
|
-
>>> signal = Signal()
|
|
166
|
-
>>>
|
|
167
|
-
>>> @signal.connect()
|
|
168
|
-
>>> def receiver(*args, **kwargs):
|
|
169
|
-
... ...
|
|
170
113
|
|
|
171
|
-
|
|
172
|
-
|
|
114
|
+
""" 期望接收数与自动删除 """
|
|
115
|
+
from CheeseSignal import Signal
|
|
173
116
|
|
|
174
|
-
|
|
175
|
-
'''
|
|
117
|
+
signal = Signal()
|
|
176
118
|
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
self._connect(fn, expected_receive_num = expected_receive_num, auto_remove = auto_remove, runType = runType)
|
|
181
|
-
return fn
|
|
182
|
-
return wrapper
|
|
119
|
+
@signal.connect(receiveNum_expected = 3)
|
|
120
|
+
def handle_1():
|
|
121
|
+
print('Handler 1 executed')
|
|
183
122
|
|
|
184
|
-
|
|
123
|
+
@signal.connect(receiveNum_expected = 3, autoRemove = True)
|
|
124
|
+
def handle_2():
|
|
125
|
+
print('Handler 2 executed')
|
|
185
126
|
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
127
|
+
if __name__ == '__main__':
|
|
128
|
+
for i in range(5):
|
|
129
|
+
signal.send()
|
|
130
|
+
print(list(signal.receivers.keys()))
|
|
131
|
+
```
|
|
132
|
+
'''
|
|
189
133
|
|
|
190
|
-
self.receivers
|
|
134
|
+
self.receivers: OrderedDict[str, Receiver] = OrderedDict()
|
|
135
|
+
''' 连接的接收器'''
|
|
136
|
+
self._sendNum: int = 0
|
|
137
|
+
''' 发送总数 '''
|
|
191
138
|
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
>>> from CheeseSignal import Signal
|
|
197
|
-
>>>
|
|
198
|
-
>>> signal = Signal()
|
|
199
|
-
>>> signal.send('data1', 'data2', **{
|
|
200
|
-
... 'key1': 'value1',
|
|
201
|
-
... 'key2': 'value2'
|
|
202
|
-
>>> })
|
|
203
|
-
'''
|
|
204
|
-
|
|
205
|
-
self._total_send_num += 1
|
|
206
|
-
|
|
207
|
-
if not self.receivers:
|
|
208
|
-
return []
|
|
209
|
-
|
|
210
|
-
results = []
|
|
211
|
-
for receiver in self.receivers[:]:
|
|
212
|
-
if receiver.active and receiver.is_unexpired:
|
|
213
|
-
receiver._total_receive_num += 1
|
|
214
|
-
results.append(receiver.fn(*args, **kwargs))
|
|
139
|
+
@overload
|
|
140
|
+
def getReceiver(self, key: str) -> Receiver | None:
|
|
141
|
+
''' 获取接收器 '''
|
|
215
142
|
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
143
|
+
@overload
|
|
144
|
+
def getReceiver(self, fn: Callable) -> Receiver | None:
|
|
145
|
+
''' 获取接收器 '''
|
|
146
|
+
|
|
147
|
+
def getReceiver(self, arg: str | Callable) -> Receiver | None:
|
|
148
|
+
if type(arg) == str:
|
|
149
|
+
return self.receivers.get(arg, None)
|
|
150
|
+
elif callable(arg):
|
|
151
|
+
for receiver in self.receivers.values():
|
|
152
|
+
if receiver.fn == arg:
|
|
153
|
+
return receiver
|
|
154
|
+
|
|
155
|
+
def _connect(self, fn: Callable, key: str | None = None, *, index: int = -1, insert: tuple[str | Callable | Receiver, Literal['BEFORE', 'AFTER']] | None = None, runType: Literal['SEQUENTIAL', 'PARALLEL', 'NO_BLOCK'] = 'SEQUENTIAL', receiveNum_expected: int = 0, autoRemove: bool = False):
|
|
156
|
+
if key in self.receivers:
|
|
157
|
+
raise ValueError(f'Receiver "{key}" already exists')
|
|
158
|
+
|
|
159
|
+
receiver = Receiver(fn, key, runType = runType, receiveNum_expected = receiveNum_expected, autoRemove = autoRemove)
|
|
160
|
+
items = list(self.receivers.items())
|
|
161
|
+
if index > -1:
|
|
162
|
+
items.insert(index, (receiver.key, receiver))
|
|
163
|
+
self.receivers.clear()
|
|
164
|
+
self.receivers.update(items)
|
|
165
|
+
elif insert:
|
|
166
|
+
if isinstance(insert[0], Receiver):
|
|
167
|
+
key = insert[0].key
|
|
168
|
+
if key not in self.receivers:
|
|
169
|
+
raise ValueError(f'Receiver "{key}" does not exist')
|
|
170
|
+
elif callable(insert[0]):
|
|
171
|
+
_receiver = self.getReceiver(insert[0])
|
|
172
|
+
if not _receiver:
|
|
173
|
+
raise ValueError(f'Receiver "{insert[0]}" does not exist')
|
|
174
|
+
key = _receiver.key
|
|
175
|
+
elif isinstance(insert[0], str):
|
|
176
|
+
key = insert[0]
|
|
177
|
+
if key not in self.receivers:
|
|
178
|
+
raise ValueError(f'Receiver "{key}" does not exist')
|
|
179
|
+
|
|
180
|
+
for i, (_key, _) in enumerate(items):
|
|
181
|
+
if _key == key:
|
|
182
|
+
if insert[1] == 'BEFORE':
|
|
183
|
+
items.insert(i, (receiver.key, receiver))
|
|
184
|
+
elif insert[1] == 'AFTER':
|
|
185
|
+
items.insert(i + 1, (receiver.key, receiver))
|
|
186
|
+
break
|
|
187
|
+
else:
|
|
188
|
+
self.receivers[receiver.key] = receiver
|
|
219
189
|
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
在协程环境中发送信号,并请保证所有接收函数都是协程函数。
|
|
223
|
-
|
|
224
|
-
>>> import asyncio
|
|
225
|
-
>>>
|
|
226
|
-
>>> from CheeseSignal import Signal
|
|
227
|
-
>>>
|
|
228
|
-
>>> async def run_asyncio():
|
|
229
|
-
... signal = Signal()
|
|
230
|
-
... await signal.async_send('data1', 'data2', **{
|
|
231
|
-
... 'key1': 'value1',
|
|
232
|
-
... 'key2': 'value2'
|
|
233
|
-
... })
|
|
234
|
-
>>>
|
|
235
|
-
>>> asyncio.run(run_asyncio())
|
|
236
|
-
|
|
237
|
-
- Returns
|
|
238
|
-
返回长度为2的元祖,分别为ORDERED类型和CONCURRENT类型的结果。
|
|
190
|
+
@overload
|
|
191
|
+
def connect(self, fn: Callable, key: str | None = None, *, index: int = -1, insert: tuple[str | Callable | Receiver, Literal['BEFORE', 'AFTER']] | None = None, runType: Literal['SEQUENTIAL', 'PARALLEL', 'NO_BLOCK'] = 'SEQUENTIAL', receiveNum_expected: int = 0, autoRemove: bool = False):
|
|
239
192
|
'''
|
|
193
|
+
连接接收器
|
|
240
194
|
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
195
|
+
- Args
|
|
196
|
+
- key: 接收器键值,若不设置则自动生成一个uuid格式的字符串
|
|
197
|
+
- runType: 运行类型
|
|
198
|
+
- SEQUENTIAL: 顺序执行,等待函数执行完成后再执行下一个函数
|
|
199
|
+
- PARALLEL: 并行执行,等待所有函数执行完成后再继续
|
|
200
|
+
- NO_BLOCK: 非阻塞执行,函数在后台执行,不等待函数执行完成
|
|
201
|
+
- receiveNum_expected: 期望接收总数
|
|
202
|
+
- autoRemove: 是否在达到期望接收总数后自动移除接收器
|
|
203
|
+
- index: 插入位置索引(仅对runType为SEQUENTIAL的接收器有效)
|
|
204
|
+
- insert: 插入位置;若设置index,则忽略此参数(仅对runType为SEQUENTIAL的接收器有效)
|
|
205
|
+
- BEFORE: 插入到指定接收器之前
|
|
206
|
+
- AFTER: 插入到指定接收器之后
|
|
207
|
+
|
|
208
|
+
- Examples
|
|
209
|
+
```python
|
|
210
|
+
from CheeseSignal import Signal
|
|
211
|
+
|
|
212
|
+
signal = Signal()
|
|
213
|
+
|
|
214
|
+
def handler():
|
|
215
|
+
print('Handler executed')
|
|
216
|
+
signal.connect(handler)
|
|
217
|
+
```
|
|
218
|
+
'''
|
|
247
219
|
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
220
|
+
@overload
|
|
221
|
+
def connect(self, key: str | None = None, *, index: int = -1, insert: tuple[str | Callable | Receiver, Literal['BEFORE', 'AFTER']] | None = None, runType: Literal['SEQUENTIAL', 'PARALLEL', 'NO_BLOCK'] = 'SEQUENTIAL', receiveNum_expected: int = 0, autoRemove: bool = False):
|
|
222
|
+
'''
|
|
223
|
+
连接接收器
|
|
252
224
|
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
225
|
+
- Args
|
|
226
|
+
- key: 接收器键值,若不设置则自动生成一个uuid格式的字符串
|
|
227
|
+
- runType: 运行类型
|
|
228
|
+
- SEQUENTIAL: 顺序执行,等待函数执行完成后再执行下一个函数
|
|
229
|
+
- PARALLEL: 并行执行,等待所有函数执行完成后再继续
|
|
230
|
+
- NO_BLOCK: 非阻塞执行,函数在后台执行,不等待函数执行完成
|
|
231
|
+
- receiveNum_expected: 期望接收总数
|
|
232
|
+
- autoRemove: 是否在达到期望接收总数后自动移除接收器
|
|
233
|
+
- index: 插入位置索引(仅对runType为SEQUENTIAL的接收器有效)
|
|
234
|
+
- insert: 插入位置;若设置index,则忽略此参数(仅对runType为SEQUENTIAL的接收器有效)
|
|
235
|
+
- BEFORE: 插入到指定接收器之前
|
|
236
|
+
- AFTER: 插入到指定接收器之后
|
|
237
|
+
|
|
238
|
+
- Examples
|
|
239
|
+
```python
|
|
240
|
+
from CheeseSignal import Signal
|
|
241
|
+
|
|
242
|
+
signal = Signal()
|
|
243
|
+
|
|
244
|
+
@signal.connect()
|
|
245
|
+
def handler():
|
|
246
|
+
print('Handler executed')
|
|
247
|
+
```
|
|
248
|
+
'''
|
|
249
|
+
|
|
250
|
+
def connect(self, arg1: Callable | str | None = None, *args, index: int = -1, insert: tuple[str | Callable | Receiver, Literal['BEFORE', 'AFTER']] | None = None, runType: Literal['SEQUENTIAL', 'PARALLEL', 'NO_BLOCK'] = 'SEQUENTIAL', receiveNum_expected: int = 0, autoRemove: bool = False):
|
|
251
|
+
def decorator(fn: Callable):
|
|
252
|
+
self._connect(fn, arg1, index = index, insert = insert, runType = runType, receiveNum_expected = receiveNum_expected, autoRemove = autoRemove)
|
|
253
|
+
return fn
|
|
254
|
+
|
|
255
|
+
if callable(arg1):
|
|
256
|
+
self._connect(arg1, *args, index = index, insert = insert, runType = runType, receiveNum_expected = receiveNum_expected, autoRemove = autoRemove)
|
|
257
|
+
else:
|
|
258
|
+
return decorator
|
|
257
259
|
|
|
258
|
-
|
|
260
|
+
@overload
|
|
261
|
+
def disconnect(self, key: str):
|
|
262
|
+
''' 断开接收器 '''
|
|
259
263
|
|
|
264
|
+
@overload
|
|
260
265
|
def disconnect(self, fn: Callable):
|
|
261
|
-
'''
|
|
262
|
-
断开接收器。
|
|
263
|
-
|
|
264
|
-
>>> from CheeseSignal import Signal
|
|
265
|
-
>>>
|
|
266
|
-
>>> def receiver(*args, **kwargs):
|
|
267
|
-
... ...
|
|
268
|
-
>>>
|
|
269
|
-
>>> signal = Signal()
|
|
270
|
-
>>> signal.connect(receiver)
|
|
271
|
-
>>> signal.disconnect(receiver)
|
|
272
|
-
|
|
273
|
-
- Raise
|
|
274
|
-
- ValueError: 未找到该函数的接收器。
|
|
275
|
-
'''
|
|
276
|
-
|
|
277
|
-
for receiver in self.receivers:
|
|
278
|
-
if receiver.fn == fn:
|
|
279
|
-
self.receivers.remove(receiver)
|
|
280
|
-
return
|
|
266
|
+
''' 断开接收器 '''
|
|
281
267
|
|
|
282
|
-
|
|
268
|
+
@overload
|
|
269
|
+
def disconnect(self, receiver: Receiver):
|
|
270
|
+
''' 断开接收器 '''
|
|
271
|
+
|
|
272
|
+
def disconnect(self, arg):
|
|
273
|
+
if isinstance(arg, str):
|
|
274
|
+
key = arg
|
|
275
|
+
if key in self.receivers:
|
|
276
|
+
del self.receivers[key]
|
|
277
|
+
elif callable(arg):
|
|
278
|
+
for key in [key for key, receiver in self.receivers.items() if receiver.fn == arg]:
|
|
279
|
+
del self.receivers[key]
|
|
280
|
+
elif isinstance(arg, Receiver):
|
|
281
|
+
if arg.key in self.receivers:
|
|
282
|
+
del self.receivers[arg.key]
|
|
283
|
+
|
|
284
|
+
def disconnectAll(self):
|
|
285
|
+
''' 断开所有接收器 '''
|
|
286
|
+
|
|
287
|
+
self.receivers.clear()
|
|
283
288
|
|
|
284
289
|
def reset(self):
|
|
285
|
-
'''
|
|
286
|
-
重置统计数据;所有的接收器的统计数据也会同步清空。
|
|
287
|
-
'''
|
|
290
|
+
''' 重置统计数据 '''
|
|
288
291
|
|
|
289
|
-
self.
|
|
290
|
-
for receiver in self.receivers:
|
|
292
|
+
self._sendNum = 0
|
|
293
|
+
for receiver in self.receivers.values():
|
|
291
294
|
receiver.reset()
|
|
292
295
|
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
断开所有接收器。
|
|
296
|
+
@overload
|
|
297
|
+
def send(self, key: str, *, args: tuple[any, ...], kwargs: dict[str, any]):
|
|
296
298
|
'''
|
|
299
|
+
发送信号
|
|
297
300
|
|
|
298
|
-
|
|
301
|
+
- Args
|
|
302
|
+
- args: *args参数
|
|
303
|
+
- kwargs: **kwargs参数
|
|
304
|
+
'''
|
|
299
305
|
|
|
300
|
-
|
|
306
|
+
@overload
|
|
307
|
+
def send(self, keys: Iterable[str], *, args: tuple[any, ...], kwargs: dict[str, any]):
|
|
301
308
|
'''
|
|
302
|
-
|
|
303
|
-
|
|
304
|
-
|
|
305
|
-
|
|
306
|
-
|
|
307
|
-
... ...
|
|
308
|
-
>>>
|
|
309
|
-
>>> signal = Signal()
|
|
310
|
-
>>> signal.connect(receiver)
|
|
311
|
-
>>>
|
|
312
|
-
>>> print(signal.get_receiver(receiver))
|
|
313
|
-
|
|
314
|
-
- Raise
|
|
315
|
-
- ValueError: 未找到该函数的接收器。
|
|
309
|
+
发送信号
|
|
310
|
+
|
|
311
|
+
- Args
|
|
312
|
+
- args: *args参数
|
|
313
|
+
- kwargs: **kwargs参数
|
|
316
314
|
'''
|
|
317
315
|
|
|
318
|
-
|
|
319
|
-
|
|
320
|
-
|
|
316
|
+
@overload
|
|
317
|
+
def send(self, *, args: tuple[any, ...], kwargs: dict[str, any]):
|
|
318
|
+
'''
|
|
319
|
+
发送信号
|
|
321
320
|
|
|
322
|
-
|
|
321
|
+
- Args
|
|
322
|
+
- args: *args参数
|
|
323
|
+
- kwargs: **kwargs参数
|
|
324
|
+
'''
|
|
325
|
+
|
|
326
|
+
def send(self, arg: str | list[str] | None = None, **kwargs):
|
|
327
|
+
if type(arg) == str:
|
|
328
|
+
self.send([arg], **kwargs)
|
|
329
|
+
elif isinstance(arg, Iterable):
|
|
330
|
+
sequential_receivers = [self.receivers[key] for key in arg if key in self.receivers and self.receivers[key].runType == 'SEQUENTIAL' and self.receivers[key].is_active]
|
|
331
|
+
if sequential_receivers:
|
|
332
|
+
for receiver in sequential_receivers:
|
|
333
|
+
self._sendHandle(receiver, **kwargs)
|
|
334
|
+
|
|
335
|
+
parallel_receivers = [self.receivers[key] for key in arg if key in self.receivers and self.receivers[key].runType == 'PARALLEL' and self.receivers[key].is_active]
|
|
336
|
+
if parallel_receivers:
|
|
337
|
+
with concurrent.futures.ThreadPoolExecutor() as executor:
|
|
338
|
+
concurrent.futures.as_completed([executor.submit(self._sendHandle, receiver, **kwargs) for receiver in parallel_receivers])
|
|
339
|
+
|
|
340
|
+
noBlock_receivers = [self.receivers[key] for key in arg if key in self.receivers and self.receivers[key].runType == 'NO_BLOCK' and self.receivers[key].is_active]
|
|
341
|
+
if noBlock_receivers:
|
|
342
|
+
with concurrent.futures.ThreadPoolExecutor() as executor:
|
|
343
|
+
for receiver in noBlock_receivers:
|
|
344
|
+
executor.submit(self._sendHandle, receiver, **kwargs)
|
|
345
|
+
else:
|
|
346
|
+
self.send(self.receivers.keys(), **kwargs)
|
|
347
|
+
|
|
348
|
+
def _sendHandle(self, receiver: Receiver, **kwargs):
|
|
349
|
+
receiver.fn(*kwargs.get('args', ()), **kwargs.get('kwargs', {}))
|
|
350
|
+
receiver._receiveNum += 1
|
|
351
|
+
if receiver.autoRemove and not receiver.is_active:
|
|
352
|
+
self.disconnect(receiver.key)
|
|
323
353
|
|
|
324
|
-
|
|
325
|
-
|
|
326
|
-
获取接收器的顺序位置;若`runType != 'ORDERED'`,则为-1。
|
|
327
|
-
|
|
328
|
-
>>> from CheeseSignal import Signal
|
|
329
|
-
>>>
|
|
330
|
-
>>> def receiver(*args, **kwargs):
|
|
331
|
-
... ...
|
|
332
|
-
>>>
|
|
333
|
-
>>> signal = Signal()
|
|
334
|
-
>>> signal.connect(receiver)
|
|
335
|
-
>>>
|
|
336
|
-
>>> print(signal.index(receiver))
|
|
337
|
-
|
|
338
|
-
- Raise
|
|
339
|
-
- ValueError: 未找到该函数的接收器。
|
|
354
|
+
@overload
|
|
355
|
+
def async_send(self, key: str, *, args: tuple[any, ...], kwargs: dict[str, any]):
|
|
340
356
|
'''
|
|
357
|
+
发送信号
|
|
341
358
|
|
|
342
|
-
|
|
343
|
-
|
|
344
|
-
|
|
359
|
+
- Args
|
|
360
|
+
- args: *args参数
|
|
361
|
+
- kwargs: **kwargs参数
|
|
362
|
+
'''
|
|
345
363
|
|
|
346
|
-
|
|
364
|
+
@overload
|
|
365
|
+
def async_send(self, keys: Iterable[str], *, args: tuple[any, ...], kwargs: dict[str, any]):
|
|
366
|
+
'''
|
|
367
|
+
发送信号
|
|
347
368
|
|
|
348
|
-
|
|
349
|
-
|
|
369
|
+
- Args
|
|
370
|
+
- args: *args参数
|
|
371
|
+
- kwargs: **kwargs参数
|
|
350
372
|
'''
|
|
351
|
-
|
|
373
|
+
|
|
374
|
+
@overload
|
|
375
|
+
def async_send(self, *, args: tuple[any, ...], kwargs: dict[str, any]):
|
|
352
376
|
'''
|
|
377
|
+
发送信号
|
|
353
378
|
|
|
354
|
-
|
|
379
|
+
- Args
|
|
380
|
+
- args: *args参数
|
|
381
|
+
- kwargs: **kwargs参数
|
|
382
|
+
'''
|
|
383
|
+
|
|
384
|
+
async def async_send(self, arg: str | list[str] | None = None, **kwargs):
|
|
385
|
+
if type(arg) == str:
|
|
386
|
+
await self.async_send([arg], **kwargs)
|
|
387
|
+
elif isinstance(arg, Iterable):
|
|
388
|
+
sequential_receivers = [self.receivers[key] for key in arg if key in self.receivers and self.receivers[key].runType == 'SEQUENTIAL' and self.receivers[key].is_active]
|
|
389
|
+
if sequential_receivers:
|
|
390
|
+
for receiver in sequential_receivers:
|
|
391
|
+
await self._async_sendHandle(receiver, **kwargs)
|
|
392
|
+
|
|
393
|
+
parallel_receivers = [self.receivers[key] for key in arg if key in self.receivers and self.receivers[key].runType == 'PARALLEL' and self.receivers[key].is_active]
|
|
394
|
+
if parallel_receivers:
|
|
395
|
+
await asyncio.gather(*[asyncio.create_task(self._async_sendHandle(receiver, **kwargs)) for receiver in parallel_receivers])
|
|
396
|
+
|
|
397
|
+
noBlock_receivers = [self.receivers[key] for key in arg if key in self.receivers and self.receivers[key].runType == 'NO_BLOCK' and self.receivers[key].is_active]
|
|
398
|
+
if noBlock_receivers:
|
|
399
|
+
for receiver in noBlock_receivers:
|
|
400
|
+
asyncio.create_task(self._async_sendHandle(receiver, **kwargs))
|
|
401
|
+
else:
|
|
402
|
+
await self.async_send(self.receivers.keys(), **kwargs)
|
|
403
|
+
|
|
404
|
+
async def _async_sendHandle(self, receiver: Receiver, **kwargs):
|
|
405
|
+
await receiver.fn(*kwargs.get('args', ()), **kwargs.get('kwargs', {}))
|
|
406
|
+
receiver._receiveNum += 1
|
|
407
|
+
if receiver.autoRemove and receiver.receiveNum_remaining == 0:
|
|
408
|
+
self.disconnect(receiver.key)
|
|
355
409
|
|
|
356
410
|
@property
|
|
357
|
-
def
|
|
358
|
-
'''
|
|
359
|
-
【只读】 总计信号发送次数。
|
|
360
|
-
'''
|
|
411
|
+
def sendNum(self) -> int:
|
|
412
|
+
''' 发送总数 '''
|
|
361
413
|
|
|
362
|
-
return self.
|
|
414
|
+
return self._sendNum
|
|
@@ -0,0 +1,82 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: CheeseSignal
|
|
3
|
+
Version: 2.0.0
|
|
4
|
+
Summary: 一款简约的信号系统。
|
|
5
|
+
Project-URL: Source, https://github.com/CheeseUnknown/CheeseSignal
|
|
6
|
+
Author-email: Cheese Unknown <cheese@cheese.ren>
|
|
7
|
+
License-File: LICENSE
|
|
8
|
+
Keywords: Signal
|
|
9
|
+
Description-Content-Type: text/markdown
|
|
10
|
+
|
|
11
|
+
# **CheeseSignal**
|
|
12
|
+
|
|
13
|
+
一款简单的信号系统。
|
|
14
|
+
|
|
15
|
+
## **示例**
|
|
16
|
+
|
|
17
|
+
### **基础用法**
|
|
18
|
+
|
|
19
|
+
```python
|
|
20
|
+
from CheeseSignal import Signal
|
|
21
|
+
|
|
22
|
+
signal = Signal()
|
|
23
|
+
|
|
24
|
+
def handle_1():
|
|
25
|
+
print('Handler 1 executed')
|
|
26
|
+
signal.connect(handle_1)
|
|
27
|
+
|
|
28
|
+
@signal.connect()
|
|
29
|
+
def handle_2():
|
|
30
|
+
print('Handler 2 executed')
|
|
31
|
+
|
|
32
|
+
if __name__ == '__main__':
|
|
33
|
+
signal.send()
|
|
34
|
+
```
|
|
35
|
+
|
|
36
|
+
### **异步用法**
|
|
37
|
+
|
|
38
|
+
```python
|
|
39
|
+
import asyncio
|
|
40
|
+
|
|
41
|
+
from CheeseSignal import Signal
|
|
42
|
+
|
|
43
|
+
signal = Signal()
|
|
44
|
+
|
|
45
|
+
async def handle_1():
|
|
46
|
+
print('Handler 1 executed')
|
|
47
|
+
signal.connect(handle_1)
|
|
48
|
+
|
|
49
|
+
@signal.connect()
|
|
50
|
+
async def handle_2():
|
|
51
|
+
print('Handler 2 executed')
|
|
52
|
+
|
|
53
|
+
if __name__ == '__main__':
|
|
54
|
+
asyncio.run(signal.async_send())
|
|
55
|
+
```
|
|
56
|
+
|
|
57
|
+
### **期望接收数与自动删除**
|
|
58
|
+
|
|
59
|
+
```python
|
|
60
|
+
from CheeseSignal import Signal
|
|
61
|
+
|
|
62
|
+
signal = Signal()
|
|
63
|
+
|
|
64
|
+
@signal.connect(receiveNum_expected = 3)
|
|
65
|
+
def handle_1():
|
|
66
|
+
print('Handler 1 executed')
|
|
67
|
+
|
|
68
|
+
@signal.connect(receiveNum_expected = 3, autoRemove = True)
|
|
69
|
+
def handle_2():
|
|
70
|
+
print('Handler 2 executed')
|
|
71
|
+
|
|
72
|
+
if __name__ == '__main__':
|
|
73
|
+
for i in range(5):
|
|
74
|
+
signal.send()
|
|
75
|
+
print(list(signal.receivers.keys()))
|
|
76
|
+
```
|
|
77
|
+
|
|
78
|
+
## **更多...**
|
|
79
|
+
|
|
80
|
+
### 1. [**Signal**](https://github.com/CheeseUnknown/CheeseSignal/blob/master/documents/Signal.md)
|
|
81
|
+
|
|
82
|
+
### 2. [**Receiver**](https://github.com/CheeseUnknown/CheeseSignal/blob/master/documents/Receiver.md)
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
CheeseSignal/__init__.py,sha256=--LvG_YyRo9N3ubHlhunm4FiiMD60hM2LsdB9uiprPM,49
|
|
2
|
+
CheeseSignal/signal.py,sha256=Sh4s2d1SEaG9cgX0EP1jO42KVcAaywJKVomhN55KO30,15380
|
|
3
|
+
cheesesignal-2.0.0.dist-info/METADATA,sha256=fVIWqdeQmQG6KZAJeBI4nuAEcehpfiWunFqHiPamzvw,1600
|
|
4
|
+
cheesesignal-2.0.0.dist-info/WHEEL,sha256=tkmg4JIqwd9H8mL30xA7crRmoStyCtGp0VWshokd1Jc,105
|
|
5
|
+
cheesesignal-2.0.0.dist-info/licenses/LICENSE,sha256=5vFb3i4UDlskszJ3jGPh8bXrM_axJfDRRuvLu1M3bIs,1070
|
|
6
|
+
cheesesignal-2.0.0.dist-info/RECORD,,
|
|
@@ -1,273 +0,0 @@
|
|
|
1
|
-
Metadata-Version: 2.3
|
|
2
|
-
Name: CheeseSignal
|
|
3
|
-
Version: 1.2.1
|
|
4
|
-
Summary: 一款简约的信号系统。
|
|
5
|
-
Project-URL: Source, https://github.com/CheeseUnknown/CheeseSignal
|
|
6
|
-
Author-email: Cheese Unknown <cheese@cheese.ren>
|
|
7
|
-
License-File: LICENSE
|
|
8
|
-
Keywords: Signal
|
|
9
|
-
Description-Content-Type: text/markdown
|
|
10
|
-
|
|
11
|
-
# **CheeseSignal**
|
|
12
|
-
|
|
13
|
-
一款简单的信号系统。
|
|
14
|
-
|
|
15
|
-
## **安装**
|
|
16
|
-
|
|
17
|
-
系统要求:Linux。
|
|
18
|
-
|
|
19
|
-
Python要求:目前仅保证支持3.11及以上版本的Python,新版本会优先支持Python的最新稳定版本。
|
|
20
|
-
|
|
21
|
-
```
|
|
22
|
-
pip install CheeseSignal
|
|
23
|
-
```
|
|
24
|
-
|
|
25
|
-
## **使用**
|
|
26
|
-
|
|
27
|
-
使用方式较为简单,通过函数或装饰件连接至某个信号,随后等待信号发送。
|
|
28
|
-
|
|
29
|
-
```python
|
|
30
|
-
import time
|
|
31
|
-
|
|
32
|
-
from CheeseSignal import signal
|
|
33
|
-
|
|
34
|
-
signal = Signal()
|
|
35
|
-
|
|
36
|
-
def receiver1(*args, **kwargs):
|
|
37
|
-
print(arg, kwargs)
|
|
38
|
-
signal.connect(receiver1)
|
|
39
|
-
|
|
40
|
-
@signal.connect()
|
|
41
|
-
def receiver2(*args, **kwargs):
|
|
42
|
-
return args, kwargs
|
|
43
|
-
|
|
44
|
-
while True:
|
|
45
|
-
print(signal.send('Hello', 'World', {
|
|
46
|
-
'Cheese': 'Signal'
|
|
47
|
-
}))
|
|
48
|
-
time.sleep(1)
|
|
49
|
-
```
|
|
50
|
-
|
|
51
|
-
## **接口文档**
|
|
52
|
-
|
|
53
|
-
### **`class Signal`**
|
|
54
|
-
|
|
55
|
-
```python
|
|
56
|
-
from CheeseSignal import Signal
|
|
57
|
-
|
|
58
|
-
signal = Signal()
|
|
59
|
-
```
|
|
60
|
-
|
|
61
|
-
#### **`self.receivers: List[Receiver]`**
|
|
62
|
-
|
|
63
|
-
【只读】 接收器的列表,按注册顺序排序。
|
|
64
|
-
|
|
65
|
-
#### **`self.total_send_num: int`**
|
|
66
|
-
|
|
67
|
-
【只读】 总计信号发送次数。
|
|
68
|
-
|
|
69
|
-
#### **`def connect(self, fn: Callable, *, expected_receive_num: int = 0, auto_remove: bool = False, runType: Literal['ORDERED', 'CONCURRENT', 'NO_WAIT'] = 'ORDERED')`**
|
|
70
|
-
|
|
71
|
-
通过函数注册响应函数。
|
|
72
|
-
|
|
73
|
-
- **参数**
|
|
74
|
-
|
|
75
|
-
- `expected_receive_num`: 期望接受信号的次数,超过该次数则不再响应信号;0为无限次。
|
|
76
|
-
|
|
77
|
-
- `auto_remove`: 是否自动删除响应次数超出期望次数的接收器。
|
|
78
|
-
|
|
79
|
-
- `runType`: 运行的方式,仅在`async_send`时有效。
|
|
80
|
-
|
|
81
|
-
- ORDERED: 按顺序执行,返回结果。
|
|
82
|
-
|
|
83
|
-
- CONCURRENT: 并行执行,返回结果。
|
|
84
|
-
|
|
85
|
-
- NO_WAIT: 并行执行,不阻塞代码。
|
|
86
|
-
|
|
87
|
-
- **报错**
|
|
88
|
-
|
|
89
|
-
- `ValueError`: 已有重复的函数接收器。
|
|
90
|
-
|
|
91
|
-
#### **`def connect(self, *, expected_receive_num: int = 0, auto_remove: bool = False, runType: Literal['ORDERED', 'CONCURRENT', 'NO_WAIT'] = 'ORDERED')`**
|
|
92
|
-
|
|
93
|
-
通过装饰器注册响应函数。
|
|
94
|
-
|
|
95
|
-
- **参数**
|
|
96
|
-
|
|
97
|
-
- `expected_receive_num`: 期望接受信号的次数,超过该次数则不再响应信号;0为无限次。
|
|
98
|
-
|
|
99
|
-
- `auto_remove`: 是否自动删除响应次数超出期望次数的接收器。
|
|
100
|
-
|
|
101
|
-
- `runType`: 运行的方式,仅在`async_send`时有效。
|
|
102
|
-
|
|
103
|
-
- ORDERED: 按顺序执行,返回结果。
|
|
104
|
-
|
|
105
|
-
- CONCURRENT: 并行执行,返回结果。
|
|
106
|
-
|
|
107
|
-
- NO_WAIT: 并行执行,不阻塞代码。
|
|
108
|
-
|
|
109
|
-
- **报错**
|
|
110
|
-
|
|
111
|
-
- `ValueError`: 已有重复的函数接收器。
|
|
112
|
-
|
|
113
|
-
#### **`def send(self, *args, **kwargs) -> List[Any]`**
|
|
114
|
-
|
|
115
|
-
发送信号。
|
|
116
|
-
|
|
117
|
-
#### **`async def async_send(self, *args, **kwargs) -> List[Any]`**
|
|
118
|
-
|
|
119
|
-
在协程环境中发送信号,并请保证所有接收函数都是协程函数。
|
|
120
|
-
|
|
121
|
-
```python
|
|
122
|
-
import asyncio
|
|
123
|
-
|
|
124
|
-
from CheeseSignal import Signal
|
|
125
|
-
|
|
126
|
-
async def run_asyncio():
|
|
127
|
-
signal = Signal()
|
|
128
|
-
await signal.async_send('data1', 'data2', **{
|
|
129
|
-
'key1': 'value1',
|
|
130
|
-
'key2': 'value2'
|
|
131
|
-
})
|
|
132
|
-
|
|
133
|
-
asyncio.run(run_asyncio())
|
|
134
|
-
```
|
|
135
|
-
|
|
136
|
-
#### **`def disconnect(self, fn: Callable)`**
|
|
137
|
-
|
|
138
|
-
断开接收器。
|
|
139
|
-
|
|
140
|
-
```python
|
|
141
|
-
from CheeseSignal import Signal
|
|
142
|
-
|
|
143
|
-
def receiver(*args, **kwargs):
|
|
144
|
-
...
|
|
145
|
-
|
|
146
|
-
signal = Signal()
|
|
147
|
-
signal.connect(receiver)
|
|
148
|
-
signal.disconnect(receiver)
|
|
149
|
-
```
|
|
150
|
-
|
|
151
|
-
- **报错**
|
|
152
|
-
|
|
153
|
-
- `ValueError`: 未找到该函数的接收器。
|
|
154
|
-
|
|
155
|
-
#### **`def reset(self)`**
|
|
156
|
-
|
|
157
|
-
重置统计数据;所有的接收器的统计数据也会同步清空。
|
|
158
|
-
|
|
159
|
-
#### **`def disconnect_all(self)`**
|
|
160
|
-
|
|
161
|
-
断开所有接收器。
|
|
162
|
-
|
|
163
|
-
#### **`def get_receiver(self, fn: Callable) -> Receiver`**
|
|
164
|
-
|
|
165
|
-
获取接收器。
|
|
166
|
-
|
|
167
|
-
```python
|
|
168
|
-
from CheeseSignal import Signal
|
|
169
|
-
|
|
170
|
-
def receiver(*args, **kwargs):
|
|
171
|
-
...
|
|
172
|
-
|
|
173
|
-
signal = Signal()
|
|
174
|
-
signal.connect(receiver)
|
|
175
|
-
|
|
176
|
-
print(signal.get_receiver(receiver))
|
|
177
|
-
```
|
|
178
|
-
|
|
179
|
-
- **报错**
|
|
180
|
-
|
|
181
|
-
- `ValueError`: 未找到该函数的接收器。
|
|
182
|
-
|
|
183
|
-
#### **`def index(self, fn: Callable) -> int`**
|
|
184
|
-
|
|
185
|
-
获取接收器的顺序位置;若`runType != 'ORDERED'`,则为-1。
|
|
186
|
-
|
|
187
|
-
```python
|
|
188
|
-
from CheeseSignal import Signal
|
|
189
|
-
|
|
190
|
-
def receiver(*args, **kwargs):
|
|
191
|
-
...
|
|
192
|
-
|
|
193
|
-
signal = Signal()
|
|
194
|
-
signal.connect(receiver)
|
|
195
|
-
|
|
196
|
-
print(signal.index(receiver))
|
|
197
|
-
```
|
|
198
|
-
|
|
199
|
-
- **报错**
|
|
200
|
-
|
|
201
|
-
- `ValueError`: 未找到该函数的接收器。
|
|
202
|
-
|
|
203
|
-
### **`class Receiver`**
|
|
204
|
-
|
|
205
|
-
正常使用并不需要手动创建该类。
|
|
206
|
-
|
|
207
|
-
```python
|
|
208
|
-
from CheeseSignal import Receiver
|
|
209
|
-
```
|
|
210
|
-
|
|
211
|
-
#### **`def __init__(self, signal: Signal, fn: Callable, *, expected_receive_num: int, auto_remove: bool, runType: Literal['ORDERED', 'CONCURRENT', 'NO_WAIT'] = 'ORDERED')`**
|
|
212
|
-
|
|
213
|
-
- **参数**
|
|
214
|
-
|
|
215
|
-
- `expected_receive_num`: 期望接受信号的次数,超过该次数则不再响应信号;0为无限次。
|
|
216
|
-
|
|
217
|
-
- `auto_remove`: 是否自动删除响应次数超出期望次数的接收器。
|
|
218
|
-
|
|
219
|
-
- `runType`: 运行的方式,仅在`async_send`时有效。
|
|
220
|
-
|
|
221
|
-
- ORDERED: 按顺序执行,返回结果。
|
|
222
|
-
|
|
223
|
-
- CONCURRENT: 并行执行,返回结果。
|
|
224
|
-
|
|
225
|
-
- NO_WAIT: 并行执行,不阻塞代码。
|
|
226
|
-
|
|
227
|
-
#### **`self.expected_receive_num: int`**
|
|
228
|
-
|
|
229
|
-
期望接受信号的次数,超过该次数则不再响应信号;0为无限次。
|
|
230
|
-
|
|
231
|
-
设置值小于`total_receive_num`且`auto_remove is True`,则会立刻删除。
|
|
232
|
-
|
|
233
|
-
#### **`self.auto_remove: bool`**
|
|
234
|
-
|
|
235
|
-
是否自动删除响应次数超出期望次数的接收器。
|
|
236
|
-
|
|
237
|
-
设置为`True`且`is_expired is True`,则会立刻删除。
|
|
238
|
-
|
|
239
|
-
#### **`self.active: bool`**
|
|
240
|
-
|
|
241
|
-
是否激活;未激活将忽略信号。
|
|
242
|
-
|
|
243
|
-
#### **`self.total_receive_num: int`**
|
|
244
|
-
|
|
245
|
-
【只读】 总计信号接受次数。
|
|
246
|
-
|
|
247
|
-
#### **`self.remaining_receive_num: int`**
|
|
248
|
-
|
|
249
|
-
【只读】 剩余的期望信号接受次数;返回为-1代表无期望信号接受次数。
|
|
250
|
-
|
|
251
|
-
#### **`self.is_expired: bool`**
|
|
252
|
-
|
|
253
|
-
【只读】 是否过期。
|
|
254
|
-
|
|
255
|
-
#### **`self.is_unexpired: bool`**
|
|
256
|
-
|
|
257
|
-
【只读】 是否未过期。
|
|
258
|
-
|
|
259
|
-
#### **`self.runType: Literal['ORDERED', 'CONCURRENT', 'NO_WAIT']`**
|
|
260
|
-
|
|
261
|
-
【只读】 运行的方式,仅在`async_send`时有效。
|
|
262
|
-
|
|
263
|
-
- ORDERED: 按顺序执行,返回结果。
|
|
264
|
-
|
|
265
|
-
- CONCURRENT: 并行执行,返回结果。
|
|
266
|
-
|
|
267
|
-
- NO_WAIT: 并行执行,不阻塞代码。
|
|
268
|
-
|
|
269
|
-
#### **`def reset(self)`**
|
|
270
|
-
|
|
271
|
-
重统计置数据。
|
|
272
|
-
|
|
273
|
-
在有期望信号接受次数的情况下,`auto_remove is False`的接收器会重新开始计数并接收信号。
|
|
@@ -1,6 +0,0 @@
|
|
|
1
|
-
CheeseSignal/__init__.py,sha256=--LvG_YyRo9N3ubHlhunm4FiiMD60hM2LsdB9uiprPM,49
|
|
2
|
-
CheeseSignal/signal.py,sha256=EqI99dbN7Wyxb7hvd3h9JQWruHeRHOKcHbXp4DwSfqs,11019
|
|
3
|
-
cheesesignal-1.2.1.dist-info/METADATA,sha256=3obooywZfLiOHcegmLzSU1tqml1ZrIZfklxI9aniaf4,6334
|
|
4
|
-
cheesesignal-1.2.1.dist-info/WHEEL,sha256=fl6v0VwpzfGBVsGtkAkhILUlJxROXbA3HvRL6Fe3140,105
|
|
5
|
-
cheesesignal-1.2.1.dist-info/licenses/LICENSE,sha256=5vFb3i4UDlskszJ3jGPh8bXrM_axJfDRRuvLu1M3bIs,1070
|
|
6
|
-
cheesesignal-1.2.1.dist-info/RECORD,,
|
|
File without changes
|