py-messor 0.2.4__tar.gz
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.
- py_messor-0.2.4/LICENSE +201 -0
- py_messor-0.2.4/PKG-INFO +311 -0
- py_messor-0.2.4/README.md +284 -0
- py_messor-0.2.4/messor/__init__.py +7 -0
- py_messor-0.2.4/messor/backends/__init__.py +1 -0
- py_messor-0.2.4/messor/backends/queue/__init__.py +1 -0
- py_messor-0.2.4/messor/backends/queue/broker.py +35 -0
- py_messor-0.2.4/messor/backends/queue/build.py +20 -0
- py_messor-0.2.4/messor/backends/queue/config.py +17 -0
- py_messor-0.2.4/messor/backends/queue/consumer.py +47 -0
- py_messor-0.2.4/messor/backends/queue/producer.py +28 -0
- py_messor-0.2.4/messor/backends/queue/resource.py +23 -0
- py_messor-0.2.4/messor/backends/rabbit_aio_pika/__init__.py +1 -0
- py_messor-0.2.4/messor/backends/rabbit_aio_pika/broker.py +46 -0
- py_messor-0.2.4/messor/backends/rabbit_aio_pika/build.py +26 -0
- py_messor-0.2.4/messor/backends/rabbit_aio_pika/config.py +55 -0
- py_messor-0.2.4/messor/backends/rabbit_aio_pika/consumer.py +77 -0
- py_messor-0.2.4/messor/backends/rabbit_aio_pika/producer.py +68 -0
- py_messor-0.2.4/messor/backends/rabbit_aio_pika/resource.py +111 -0
- py_messor-0.2.4/messor/core/__init__.py +1 -0
- py_messor-0.2.4/messor/core/_inflight.py +74 -0
- py_messor-0.2.4/messor/core/broker.py +63 -0
- py_messor-0.2.4/messor/core/config.py +35 -0
- py_messor-0.2.4/messor/core/consumer.py +121 -0
- py_messor-0.2.4/messor/core/producer.py +90 -0
- py_messor-0.2.4/messor/core/resource.py +25 -0
- py_messor-0.2.4/messor/errors.py +7 -0
- py_messor-0.2.4/messor/factory/__init__.py +11 -0
- py_messor-0.2.4/messor/factory/app.py +135 -0
- py_messor-0.2.4/messor/factory/backend.py +14 -0
- py_messor-0.2.4/messor/factory/deco.py +48 -0
- py_messor-0.2.4/messor/factory/registry.py +52 -0
- py_messor-0.2.4/messor/pool_executor/__init__.py +1 -0
- py_messor-0.2.4/messor/pool_executor/base.py +31 -0
- py_messor-0.2.4/messor/pool_executor/general.py +42 -0
- py_messor-0.2.4/messor/serializer/__init__.py +1 -0
- py_messor-0.2.4/messor/serializer/base.py +27 -0
- py_messor-0.2.4/messor/serializer/orjson_ser.py +27 -0
- py_messor-0.2.4/messor/serializer/raw.py +24 -0
- py_messor-0.2.4/messor/task/__init__.py +1 -0
- py_messor-0.2.4/messor/task/base.py +28 -0
- py_messor-0.2.4/messor/task/thread.py +26 -0
- py_messor-0.2.4/messor/utils/__init__.py +1 -0
- py_messor-0.2.4/messor/utils/deco.py +36 -0
- py_messor-0.2.4/messor/utils/hook.py +121 -0
- py_messor-0.2.4/messor/utils/lazy.py +46 -0
- py_messor-0.2.4/messor/utils/sem.py +49 -0
- py_messor-0.2.4/messor/utils/timer.py +34 -0
- py_messor-0.2.4/messor/utils/waiter.py +35 -0
- py_messor-0.2.4/pyproject.toml +44 -0
py_messor-0.2.4/LICENSE
ADDED
|
@@ -0,0 +1,201 @@
|
|
|
1
|
+
Apache License
|
|
2
|
+
Version 2.0, January 2004
|
|
3
|
+
http://www.apache.org/licenses/
|
|
4
|
+
|
|
5
|
+
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
|
6
|
+
|
|
7
|
+
1. Definitions.
|
|
8
|
+
|
|
9
|
+
"License" shall mean the terms and conditions for use, reproduction,
|
|
10
|
+
and distribution as defined by Sections 1 through 9 of this document.
|
|
11
|
+
|
|
12
|
+
"Licensor" shall mean the copyright owner or entity authorized by
|
|
13
|
+
the copyright owner that is granting the License.
|
|
14
|
+
|
|
15
|
+
"Legal Entity" shall mean the union of the acting entity and all
|
|
16
|
+
other entities that control, are controlled by, or are under common
|
|
17
|
+
control with that entity. For the purposes of this definition,
|
|
18
|
+
"control" means (i) the power, direct or indirect, to cause the
|
|
19
|
+
direction or management of such entity, whether by contract or
|
|
20
|
+
otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
|
21
|
+
outstanding shares, or (iii) beneficial ownership of such entity.
|
|
22
|
+
|
|
23
|
+
"You" (or "Your") shall mean an individual or Legal Entity
|
|
24
|
+
exercising permissions granted by this License.
|
|
25
|
+
|
|
26
|
+
"Source" form shall mean the preferred form for making modifications,
|
|
27
|
+
including but not limited to software source code, documentation
|
|
28
|
+
source, and configuration files.
|
|
29
|
+
|
|
30
|
+
"Object" form shall mean any form resulting from mechanical
|
|
31
|
+
transformation or translation of a Source form, including but
|
|
32
|
+
not limited to compiled object code, generated documentation,
|
|
33
|
+
and conversions to other media types.
|
|
34
|
+
|
|
35
|
+
"Work" shall mean the work of authorship, whether in Source or
|
|
36
|
+
Object form, made available under the License, as indicated by a
|
|
37
|
+
copyright notice that is included in or attached to the work
|
|
38
|
+
(an example is provided in the Appendix below).
|
|
39
|
+
|
|
40
|
+
"Derivative Works" shall mean any work, whether in Source or Object
|
|
41
|
+
form, that is based on (or derived from) the Work and for which the
|
|
42
|
+
editorial revisions, annotations, elaborations, or other modifications
|
|
43
|
+
represent, as a whole, an original work of authorship. For the purposes
|
|
44
|
+
of this License, Derivative Works shall not include works that remain
|
|
45
|
+
separable from, or merely link (or bind by name) to the interfaces of,
|
|
46
|
+
the Work and Derivative Works thereof.
|
|
47
|
+
|
|
48
|
+
"Contribution" shall mean any work of authorship, including
|
|
49
|
+
the original version of the Work and any modifications or additions
|
|
50
|
+
to that Work or Derivative Works thereof, that is intentionally
|
|
51
|
+
submitted to Licensor for inclusion in the Work by the copyright owner
|
|
52
|
+
or by an individual or Legal Entity authorized to submit on behalf of
|
|
53
|
+
the copyright owner. For the purposes of this definition, "submitted"
|
|
54
|
+
means any form of electronic, verbal, or written communication sent
|
|
55
|
+
to the Licensor or its representatives, including but not limited to
|
|
56
|
+
communication on electronic mailing lists, source code control systems,
|
|
57
|
+
and issue tracking systems that are managed by, or on behalf of, the
|
|
58
|
+
Licensor for the purpose of discussing and improving the Work, but
|
|
59
|
+
excluding communication that is conspicuously marked or otherwise
|
|
60
|
+
designated in writing by the copyright owner as "Not a Contribution."
|
|
61
|
+
|
|
62
|
+
"Contributor" shall mean Licensor and any individual or Legal Entity
|
|
63
|
+
on behalf of whom a Contribution has been received by Licensor and
|
|
64
|
+
subsequently incorporated within the Work.
|
|
65
|
+
|
|
66
|
+
2. Grant of Copyright License. Subject to the terms and conditions of
|
|
67
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
68
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
69
|
+
copyright license to reproduce, prepare Derivative Works of,
|
|
70
|
+
publicly display, publicly perform, sublicense, and distribute the
|
|
71
|
+
Work and such Derivative Works in Source or Object form.
|
|
72
|
+
|
|
73
|
+
3. Grant of Patent License. Subject to the terms and conditions of
|
|
74
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
75
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
76
|
+
(except as stated in this section) patent license to make, have made,
|
|
77
|
+
use, offer to sell, sell, import, and otherwise transfer the Work,
|
|
78
|
+
where such license applies only to those patent claims licensable
|
|
79
|
+
by such Contributor that are necessarily infringed by their
|
|
80
|
+
Contribution(s) alone or by combination of their Contribution(s)
|
|
81
|
+
with the Work to which such Contribution(s) was submitted. If You
|
|
82
|
+
institute patent litigation against any entity (including a
|
|
83
|
+
cross-claim or counterclaim in a lawsuit) alleging that the Work
|
|
84
|
+
or a Contribution incorporated within the Work constitutes direct
|
|
85
|
+
or contributory patent infringement, then any patent licenses
|
|
86
|
+
granted to You under this License for that Work shall terminate
|
|
87
|
+
as of the date such litigation is filed.
|
|
88
|
+
|
|
89
|
+
4. Redistribution. You may reproduce and distribute copies of the
|
|
90
|
+
Work or Derivative Works thereof in any medium, with or without
|
|
91
|
+
modifications, and in Source or Object form, provided that You
|
|
92
|
+
meet the following conditions:
|
|
93
|
+
|
|
94
|
+
(a) You must give any other recipients of the Work or
|
|
95
|
+
Derivative Works a copy of this License; and
|
|
96
|
+
|
|
97
|
+
(b) You must cause any modified files to carry prominent notices
|
|
98
|
+
stating that You changed the files; and
|
|
99
|
+
|
|
100
|
+
(c) You must retain, in the Source form of any Derivative Works
|
|
101
|
+
that You distribute, all copyright, patent, trademark, and
|
|
102
|
+
attribution notices from the Source form of the Work,
|
|
103
|
+
excluding those notices that do not pertain to any part of
|
|
104
|
+
the Derivative Works; and
|
|
105
|
+
|
|
106
|
+
(d) If the Work includes a "NOTICE" text file as part of its
|
|
107
|
+
distribution, then any Derivative Works that You distribute must
|
|
108
|
+
include a readable copy of the attribution notices contained
|
|
109
|
+
within such NOTICE file, excluding those notices that do not
|
|
110
|
+
pertain to any part of the Derivative Works, in at least one
|
|
111
|
+
of the following places: within a NOTICE text file distributed
|
|
112
|
+
as part of the Derivative Works; within the Source form or
|
|
113
|
+
documentation, if provided along with the Derivative Works; or,
|
|
114
|
+
within a display generated by the Derivative Works, if and
|
|
115
|
+
wherever such third-party notices normally appear. The contents
|
|
116
|
+
of the NOTICE file are for informational purposes only and
|
|
117
|
+
do not modify the License. You may add Your own attribution
|
|
118
|
+
notices within Derivative Works that You distribute, alongside
|
|
119
|
+
or as an addendum to the NOTICE text from the Work, provided
|
|
120
|
+
that such additional attribution notices cannot be construed
|
|
121
|
+
as modifying the License.
|
|
122
|
+
|
|
123
|
+
You may add Your own copyright statement to Your modifications and
|
|
124
|
+
may provide additional or different license terms and conditions
|
|
125
|
+
for use, reproduction, or distribution of Your modifications, or
|
|
126
|
+
for any such Derivative Works as a whole, provided Your use,
|
|
127
|
+
reproduction, and distribution of the Work otherwise complies with
|
|
128
|
+
the conditions stated in this License.
|
|
129
|
+
|
|
130
|
+
5. Submission of Contributions. Unless You explicitly state otherwise,
|
|
131
|
+
any Contribution intentionally submitted for inclusion in the Work
|
|
132
|
+
by You to the Licensor shall be under the terms and conditions of
|
|
133
|
+
this License, without any additional terms or conditions.
|
|
134
|
+
Notwithstanding the above, nothing herein shall supersede or modify
|
|
135
|
+
the terms of any separate license agreement you may have executed
|
|
136
|
+
with Licensor regarding such Contributions.
|
|
137
|
+
|
|
138
|
+
6. Trademarks. This License does not grant permission to use the trade
|
|
139
|
+
names, trademarks, service marks, or product names of the Licensor,
|
|
140
|
+
except as required for reasonable and customary use in describing the
|
|
141
|
+
origin of the Work and reproducing the content of the NOTICE file.
|
|
142
|
+
|
|
143
|
+
7. Disclaimer of Warranty. Unless required by applicable law or
|
|
144
|
+
agreed to in writing, Licensor provides the Work (and each
|
|
145
|
+
Contributor provides its Contributions) on an "AS IS" BASIS,
|
|
146
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
|
147
|
+
implied, including, without limitation, any warranties or conditions
|
|
148
|
+
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
|
|
149
|
+
PARTICULAR PURPOSE. You are solely responsible for determining the
|
|
150
|
+
appropriateness of using or redistributing the Work and assume any
|
|
151
|
+
risks associated with Your exercise of permissions under this License.
|
|
152
|
+
|
|
153
|
+
8. Limitation of Liability. In no event and under no legal theory,
|
|
154
|
+
whether in tort (including negligence), contract, or otherwise,
|
|
155
|
+
unless required by applicable law (such as deliberate and grossly
|
|
156
|
+
negligent acts) or agreed to in writing, shall any Contributor be
|
|
157
|
+
liable to You for damages, including any direct, indirect, special,
|
|
158
|
+
incidental, or consequential damages of any character arising as a
|
|
159
|
+
result of this License or out of the use or inability to use the
|
|
160
|
+
Work (including but not limited to damages for loss of goodwill,
|
|
161
|
+
work stoppage, computer failure or malfunction, or any and all
|
|
162
|
+
other commercial damages or losses), even if such Contributor
|
|
163
|
+
has been advised of the possibility of such damages.
|
|
164
|
+
|
|
165
|
+
9. Accepting Warranty or Additional Liability. While redistributing
|
|
166
|
+
the Work or Derivative Works thereof, You may choose to offer,
|
|
167
|
+
and charge a fee for, acceptance of support, warranty, indemnity,
|
|
168
|
+
or other liability obligations and/or rights consistent with this
|
|
169
|
+
License. However, in accepting such obligations, You may act only
|
|
170
|
+
on Your own behalf and on Your sole responsibility, not on behalf
|
|
171
|
+
of any other Contributor, and only if You agree to indemnify,
|
|
172
|
+
defend, and hold each Contributor harmless for any liability
|
|
173
|
+
incurred by, or claims asserted against, such Contributor by reason
|
|
174
|
+
of your accepting any such warranty or additional liability.
|
|
175
|
+
|
|
176
|
+
END OF TERMS AND CONDITIONS
|
|
177
|
+
|
|
178
|
+
APPENDIX: How to apply the Apache License to your work.
|
|
179
|
+
|
|
180
|
+
To apply the Apache License to your work, attach the following
|
|
181
|
+
boilerplate notice, with the fields enclosed by brackets "[]"
|
|
182
|
+
replaced with your own identifying information. (Don't include
|
|
183
|
+
the brackets!) The text should be enclosed in the appropriate
|
|
184
|
+
comment syntax for the file format. We also recommend that a
|
|
185
|
+
file or class name and description of purpose be included on the
|
|
186
|
+
same "printed page" as the copyright notice for easier
|
|
187
|
+
identification within third-party archives.
|
|
188
|
+
|
|
189
|
+
Copyright [yyyy] [name of copyright owner]
|
|
190
|
+
|
|
191
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
|
192
|
+
you may not use this file except in compliance with the License.
|
|
193
|
+
You may obtain a copy of the License at
|
|
194
|
+
|
|
195
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
|
196
|
+
|
|
197
|
+
Unless required by applicable law or agreed to in writing, software
|
|
198
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
|
199
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
200
|
+
See the License for the specific language governing permissions and
|
|
201
|
+
limitations under the License.
|
py_messor-0.2.4/PKG-INFO
ADDED
|
@@ -0,0 +1,311 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: py-messor
|
|
3
|
+
Version: 0.2.4
|
|
4
|
+
Summary: Async coroutine functions as queue-backed tasks (QueueFunc).
|
|
5
|
+
License: Apache-2.0
|
|
6
|
+
License-File: LICENSE
|
|
7
|
+
Author: liuseki
|
|
8
|
+
Author-email: liuseki@163.com
|
|
9
|
+
Requires-Python: >=3.12,<4.0
|
|
10
|
+
Classifier: License :: OSI Approved :: Apache Software License
|
|
11
|
+
Classifier: Programming Language :: Python :: 3
|
|
12
|
+
Classifier: Programming Language :: Python :: 3.12
|
|
13
|
+
Classifier: Programming Language :: Python :: 3.13
|
|
14
|
+
Classifier: Programming Language :: Python :: 3.14
|
|
15
|
+
Classifier: Programming Language :: Python :: 3.15
|
|
16
|
+
Provides-Extra: rabbit
|
|
17
|
+
Requires-Dist: aio-pika (>=9.6,<10.0) ; extra == "rabbit"
|
|
18
|
+
Requires-Dist: orjson
|
|
19
|
+
Requires-Dist: pydantic (>=2.14.1,<3.0.0)
|
|
20
|
+
Project-URL: Changelog, https://github.com/sekil9529/py-messor/blob/master/CHANGELOG.md
|
|
21
|
+
Project-URL: Documentation, https://github.com/sekil9529/py-messor#readme
|
|
22
|
+
Project-URL: Homepage, https://github.com/sekil9529/py-messor
|
|
23
|
+
Project-URL: Issues, https://github.com/sekil9529/py-messor/issues
|
|
24
|
+
Project-URL: Repository, https://github.com/sekil9529/py-messor
|
|
25
|
+
Description-Content-Type: text/markdown
|
|
26
|
+
|
|
27
|
+
# messor
|
|
28
|
+
|
|
29
|
+
[](https://coveralls.io/github/sekil9529/py-messor)
|
|
30
|
+
[](https://github.com/sekil9529/py-messor/actions?query=workflow%3Atests)
|
|
31
|
+
[](https://pypi.org/project/py-messor/)
|
|
32
|
+
[](https://pypi.org/project/py-messor/)
|
|
33
|
+
[](https://pypi.org/project/py-messor/)
|
|
34
|
+
[](https://pypi.org/project/py-messor/)
|
|
35
|
+
|
|
36
|
+
把一个 `async def` 收成队列任务:`push` 投递,消费者在同一个事件循环里执行。
|
|
37
|
+
|
|
38
|
+
仓库示例在 [examples/](examples/)。版本记录见 [CHANGELOG.md](CHANGELOG.md)。
|
|
39
|
+
|
|
40
|
+
> 需要 Python 3.12 及以上。任务必须是协程函数。
|
|
41
|
+
>
|
|
42
|
+
> RabbitMQ 后端是可选依赖,底层用 [aio-pika](https://github.com/mosquito/aio-pika) `^9.6`。
|
|
43
|
+
|
|
44
|
+
## 特点
|
|
45
|
+
|
|
46
|
+
- 进程内 `asyncio.Queue` 和 RabbitMQ 两种后端,统一用 `App` 的 `@app.queue` 装饰。
|
|
47
|
+
- 每个任务有自己的队列、序列化和消费并发上限。
|
|
48
|
+
- `App.start()` 逆序启动消费者(下游先就绪),`App.stop()` 按关闭编排回收。
|
|
49
|
+
- `start(queues=[...])` 可以只启动部分消费者;绑定的内存队列必须全部包含,否则报错。
|
|
50
|
+
- `start` 会等到消费者开始收消息再返回。
|
|
51
|
+
- 装饰之后仍可以直接 `await` 调用,不经过队列。
|
|
52
|
+
|
|
53
|
+
## 启动和停止
|
|
54
|
+
|
|
55
|
+
`app.start()` 启动消费者:内存队列 consumer 先按逆序启动,其余队列随后也按逆序启动(下游先就绪)。`start` 会等到消费者开始收消息再返回——内存队列已经堵在 `get()` 上,RabbitMQ 是队列已经订阅上。
|
|
56
|
+
|
|
57
|
+
`app.stop()` 按编排关闭:
|
|
58
|
+
|
|
59
|
+
1. 非内存队列 consumer 逐个 stop/join——先断掉往内存队列投递的外部源;
|
|
60
|
+
2. `wait_drained` 等全部在途消息处理完成(此时内存 consumer 仍在消费);
|
|
61
|
+
3. 内存队列 consumer 逐个 stop/join;
|
|
62
|
+
4. 生产者全部 stop/join;绑定但未启动的 producer 也按绑定顺序一并收尾。
|
|
63
|
+
|
|
64
|
+
单个 consumer 的 stop 先标上停止,再等手头这次收消息结束;空闲时最多再等一个 `consume_timeout`。还没取走的 RabbitMQ 消息留在代理上。
|
|
65
|
+
|
|
66
|
+
RabbitMQ 在消息交给消费池之后确认。处理函数出错不会把消息退回队列。`push` 会等发布确认,超时抛出 `messor.PublishTimeoutError`。
|
|
67
|
+
|
|
68
|
+
## 安装
|
|
69
|
+
|
|
70
|
+
```shell
|
|
71
|
+
pip install py-messor
|
|
72
|
+
```
|
|
73
|
+
|
|
74
|
+
使用 RabbitMQ:
|
|
75
|
+
|
|
76
|
+
```shell
|
|
77
|
+
pip install "py-messor[rabbit]"
|
|
78
|
+
```
|
|
79
|
+
|
|
80
|
+
从仓库安装:
|
|
81
|
+
|
|
82
|
+
```shell
|
|
83
|
+
git clone https://github.com/sekil9529/py-messor.git
|
|
84
|
+
cd py-messor
|
|
85
|
+
poetry install -E rabbit
|
|
86
|
+
```
|
|
87
|
+
|
|
88
|
+
## 用法
|
|
89
|
+
|
|
90
|
+
从 `messor.factory` 引入 `App`、`Backend`。`name` 必填且唯一,用来在 `start(queues=[...])` 里点名。
|
|
91
|
+
|
|
92
|
+
### 进程内队列
|
|
93
|
+
|
|
94
|
+
```python
|
|
95
|
+
import asyncio
|
|
96
|
+
|
|
97
|
+
from messor.factory import App, Backend
|
|
98
|
+
|
|
99
|
+
app = App()
|
|
100
|
+
|
|
101
|
+
|
|
102
|
+
@app.queue(Backend.QUEUE, name="process_order", consume_timeout=0.5, maxsize=1000)
|
|
103
|
+
async def process_order(order_id: int) -> None:
|
|
104
|
+
print(order_id)
|
|
105
|
+
|
|
106
|
+
|
|
107
|
+
async def main() -> None:
|
|
108
|
+
await app.start()
|
|
109
|
+
await process_order.push(args=(42,))
|
|
110
|
+
await app.stop()
|
|
111
|
+
|
|
112
|
+
|
|
113
|
+
asyncio.run(main())
|
|
114
|
+
```
|
|
115
|
+
|
|
116
|
+
`push` 就是 `broker.publish`。不传 `timeout` 时用配置里的 `publish_timeout`;传入数字只对这一次有效。`None` 表示不限时。
|
|
117
|
+
|
|
118
|
+
直接调用:
|
|
119
|
+
|
|
120
|
+
```python
|
|
121
|
+
await process_order(1)
|
|
122
|
+
```
|
|
123
|
+
|
|
124
|
+
### 多段流水线
|
|
125
|
+
|
|
126
|
+
`App.start()` 逆序启动消费者:内存队列 consumer 先按逆序启动,其余队列随后也按逆序启动,下游先就绪。`stop()` 复用关闭编排:非内存 consumer 先停 -> wait_drained -> 内存 consumer -> producer。
|
|
127
|
+
|
|
128
|
+
```python
|
|
129
|
+
import asyncio
|
|
130
|
+
|
|
131
|
+
from messor.factory import App, Backend
|
|
132
|
+
|
|
133
|
+
app = App()
|
|
134
|
+
|
|
135
|
+
|
|
136
|
+
@app.queue(Backend.QUEUE, name="task_b", consumer_pool_maxsize=4)
|
|
137
|
+
async def task_b(job: str) -> None:
|
|
138
|
+
print(job)
|
|
139
|
+
|
|
140
|
+
|
|
141
|
+
@app.queue(Backend.QUEUE, name="task_a", consumer_pool_maxsize=2)
|
|
142
|
+
async def task_a(job: str) -> None:
|
|
143
|
+
await task_b.push(args=(job,))
|
|
144
|
+
|
|
145
|
+
|
|
146
|
+
async def main() -> None:
|
|
147
|
+
await app.start()
|
|
148
|
+
await task_a.push(args=("job-1",))
|
|
149
|
+
await app.stop()
|
|
150
|
+
|
|
151
|
+
|
|
152
|
+
asyncio.run(main())
|
|
153
|
+
```
|
|
154
|
+
|
|
155
|
+
三段链式示例:
|
|
156
|
+
|
|
157
|
+
```shell
|
|
158
|
+
poetry run python examples/demo_queue.py
|
|
159
|
+
```
|
|
160
|
+
|
|
161
|
+
### 只启动部分队列
|
|
162
|
+
|
|
163
|
+
`app.start(queues=[name, ...])` 只启动点名的那几个消费者,其余队列的 consumer 不启动。不传 `queues` 则全部启动。
|
|
164
|
+
|
|
165
|
+
- 绑定的内存队列必须全部包含在 `queues` 里,否则报错(内存队列没有独立持久化,漏启会导致投递无处消费)。
|
|
166
|
+
- 未启动队列的 producer 依然可用,`app.stop()` 收尾时会按绑定顺序把它一并 stop/join。
|
|
167
|
+
|
|
168
|
+
示例(内存 `task_a`/`task_b` 必须全选,rabbit 只启动 `task_r1`):
|
|
169
|
+
|
|
170
|
+
```python
|
|
171
|
+
import asyncio
|
|
172
|
+
|
|
173
|
+
from messor.factory import App, Backend
|
|
174
|
+
|
|
175
|
+
app = App()
|
|
176
|
+
|
|
177
|
+
|
|
178
|
+
@app.queue(Backend.QUEUE, name="task_a", consume_timeout=0.5)
|
|
179
|
+
async def task_a(job: str) -> None:
|
|
180
|
+
print("a", job)
|
|
181
|
+
await task_b.push(args=(job,))
|
|
182
|
+
|
|
183
|
+
|
|
184
|
+
@app.queue(Backend.QUEUE, name="task_b", consume_timeout=0.5)
|
|
185
|
+
async def task_b(job: str) -> None:
|
|
186
|
+
print("b", job)
|
|
187
|
+
|
|
188
|
+
|
|
189
|
+
@app.queue(
|
|
190
|
+
Backend.RABBIT_AIO_PIKA,
|
|
191
|
+
name="task_r1",
|
|
192
|
+
url="amqp://guest:guest@127.0.0.1:5672/",
|
|
193
|
+
queue_name="demo_partial_r1",
|
|
194
|
+
enable_declare_queue=True,
|
|
195
|
+
consume_timeout=0.5,
|
|
196
|
+
)
|
|
197
|
+
async def task_r1(job: str) -> None:
|
|
198
|
+
print("r1", job)
|
|
199
|
+
|
|
200
|
+
|
|
201
|
+
async def main() -> None:
|
|
202
|
+
await app.start(queues=["task_a", "task_b", "task_r1"])
|
|
203
|
+
await task_a.push(args=("订单-001",))
|
|
204
|
+
await asyncio.sleep(1)
|
|
205
|
+
await app.stop()
|
|
206
|
+
|
|
207
|
+
|
|
208
|
+
asyncio.run(main())
|
|
209
|
+
```
|
|
210
|
+
|
|
211
|
+
完整示例:
|
|
212
|
+
|
|
213
|
+
```shell
|
|
214
|
+
poetry run python examples/demo_partial_start.py
|
|
215
|
+
```
|
|
216
|
+
|
|
217
|
+
### RabbitMQ
|
|
218
|
+
|
|
219
|
+
`url` 和 `queue_name` 必填。`enable_declare_queue=True` 时,队列不存在会创建。默认交换机名是 `""`,路由键默认用队列名。已经存在的队列再声明时,持久化等参数必须和代理上的一致。
|
|
220
|
+
|
|
221
|
+
```python
|
|
222
|
+
import asyncio
|
|
223
|
+
|
|
224
|
+
from messor.factory import App, Backend
|
|
225
|
+
|
|
226
|
+
app = App()
|
|
227
|
+
|
|
228
|
+
|
|
229
|
+
@app.queue(
|
|
230
|
+
Backend.RABBIT_AIO_PIKA,
|
|
231
|
+
name="process_order",
|
|
232
|
+
url="amqp://guest:guest@127.0.0.1/",
|
|
233
|
+
queue_name="process_order",
|
|
234
|
+
enable_declare_queue=True,
|
|
235
|
+
consume_timeout=0.5,
|
|
236
|
+
)
|
|
237
|
+
async def process_order(order_id: int) -> None:
|
|
238
|
+
print(order_id)
|
|
239
|
+
|
|
240
|
+
|
|
241
|
+
async def main() -> None:
|
|
242
|
+
await app.start()
|
|
243
|
+
await process_order.push(args=(42,))
|
|
244
|
+
await app.stop()
|
|
245
|
+
|
|
246
|
+
|
|
247
|
+
asyncio.run(main())
|
|
248
|
+
```
|
|
249
|
+
|
|
250
|
+
本地 RabbitMQ(`127.0.0.1:5672`,`guest` / `guest`)上的链式示例:
|
|
251
|
+
|
|
252
|
+
```shell
|
|
253
|
+
poetry run python examples/demo_rabbit_aio_pika.py
|
|
254
|
+
```
|
|
255
|
+
|
|
256
|
+
常用参数:
|
|
257
|
+
|
|
258
|
+
| 参数 | 作用 |
|
|
259
|
+
|------|------|
|
|
260
|
+
| `consumer_pool_maxsize` | 同时执行的协程数,默认 200 |
|
|
261
|
+
| `consume_timeout` | 一次等消息的时间,默认 2 秒 |
|
|
262
|
+
| `publish_timeout` | 投递等待上限,默认不限 |
|
|
263
|
+
| `maxsize` | 仅内存队列,`asyncio.Queue` 容量,默认 200 |
|
|
264
|
+
| `serializer_cls` | 队列中的数据结构。内存队列默认 `raw`,RabbitMQ 默认 orjson |
|
|
265
|
+
| `prefetch_count` | 仅 RabbitMQ,未确认消息窗口 |
|
|
266
|
+
| `delivery_mode` | 仅 RabbitMQ,默认持久化 |
|
|
267
|
+
| `exchange_name` / `routing_key` | 仅 RabbitMQ |
|
|
268
|
+
| `queue_durable` | 仅 RabbitMQ,声明队列时是否持久化,默认否 |
|
|
269
|
+
|
|
270
|
+
其余字段见 `messor.backends.queue.config.Config` 和 `messor.backends.rabbit_aio_pika.config.Config`。
|
|
271
|
+
|
|
272
|
+
## 一条任务里有什么
|
|
273
|
+
|
|
274
|
+
`@app.queue` 装饰的协程就是一条队列。`push` 投到它自己这条队列上。
|
|
275
|
+
|
|
276
|
+
- **Broker** 组装生产者、消费者和双方共用的资源,并提供启停与投递。
|
|
277
|
+
- **Producer** 序列化后把数据写入队列。
|
|
278
|
+
- **Consumer** 取出数据,反序列化后交给消费池。
|
|
279
|
+
- **PoolExecutor** 按并发上限执行业务协程。
|
|
280
|
+
|
|
281
|
+
```mermaid
|
|
282
|
+
flowchart TB
|
|
283
|
+
PUSH["投递"] --> B["通道"]
|
|
284
|
+
B --> PRO["生产者"]
|
|
285
|
+
B -.->|启停| CON["消费者"]
|
|
286
|
+
|
|
287
|
+
PRO -->|序列化| Q[("队列")]
|
|
288
|
+
Q -->|反序列化| CON
|
|
289
|
+
CON --> POOL["消费池"]
|
|
290
|
+
POOL --> FN["业务协程"]
|
|
291
|
+
```
|
|
292
|
+
|
|
293
|
+
## 测试
|
|
294
|
+
|
|
295
|
+
```shell
|
|
296
|
+
poetry install -E rabbit
|
|
297
|
+
poetry run pytest
|
|
298
|
+
```
|
|
299
|
+
|
|
300
|
+
单元测试使用假的 RabbitMQ 连接,不要求本机有代理。`examples/demo_rabbit_aio_pika.py` 需要本机 `guest` / `guest`、端口 `5672`。
|
|
301
|
+
|
|
302
|
+
`pytest.ini` 会写出覆盖率页面 `htmlcov/` 和 `report.html`。
|
|
303
|
+
|
|
304
|
+
## 版本
|
|
305
|
+
|
|
306
|
+
遵循[语义化版本](https://semver.org/lang/zh-CN/)。变更记录在 [CHANGELOG.md](CHANGELOG.md)。
|
|
307
|
+
|
|
308
|
+
## 许可证
|
|
309
|
+
|
|
310
|
+
Apache-2.0
|
|
311
|
+
|