aio-scrapy 2.1.4__py3-none-any.whl → 2.1.7__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.
Files changed (112) hide show
  1. {aio_scrapy-2.1.4.dist-info → aio_scrapy-2.1.7.dist-info}/LICENSE +1 -1
  2. aio_scrapy-2.1.7.dist-info/METADATA +147 -0
  3. aio_scrapy-2.1.7.dist-info/RECORD +134 -0
  4. {aio_scrapy-2.1.4.dist-info → aio_scrapy-2.1.7.dist-info}/WHEEL +1 -1
  5. aioscrapy/VERSION +1 -1
  6. aioscrapy/cmdline.py +438 -5
  7. aioscrapy/core/downloader/__init__.py +522 -17
  8. aioscrapy/core/downloader/handlers/__init__.py +187 -5
  9. aioscrapy/core/downloader/handlers/aiohttp.py +190 -6
  10. aioscrapy/core/downloader/handlers/curl_cffi.py +126 -5
  11. aioscrapy/core/downloader/handlers/httpx.py +135 -5
  12. aioscrapy/core/downloader/handlers/pyhttpx.py +137 -5
  13. aioscrapy/core/downloader/handlers/requests.py +120 -2
  14. aioscrapy/core/downloader/handlers/webdriver/__init__.py +2 -0
  15. aioscrapy/core/downloader/handlers/webdriver/drissionpage.py +493 -0
  16. aioscrapy/core/downloader/handlers/webdriver/driverpool.py +234 -0
  17. aioscrapy/core/downloader/handlers/webdriver/playwright.py +498 -0
  18. aioscrapy/core/engine.py +381 -20
  19. aioscrapy/core/scheduler.py +350 -36
  20. aioscrapy/core/scraper.py +509 -33
  21. aioscrapy/crawler.py +392 -10
  22. aioscrapy/db/__init__.py +149 -0
  23. aioscrapy/db/absmanager.py +212 -6
  24. aioscrapy/db/aiomongo.py +292 -10
  25. aioscrapy/db/aiomysql.py +363 -10
  26. aioscrapy/db/aiopg.py +299 -2
  27. aioscrapy/db/aiorabbitmq.py +444 -4
  28. aioscrapy/db/aioredis.py +260 -11
  29. aioscrapy/dupefilters/__init__.py +110 -5
  30. aioscrapy/dupefilters/disk.py +124 -2
  31. aioscrapy/dupefilters/redis.py +598 -32
  32. aioscrapy/exceptions.py +151 -13
  33. aioscrapy/http/__init__.py +1 -1
  34. aioscrapy/http/headers.py +237 -3
  35. aioscrapy/http/request/__init__.py +257 -11
  36. aioscrapy/http/request/form.py +83 -3
  37. aioscrapy/http/request/json_request.py +121 -9
  38. aioscrapy/http/response/__init__.py +306 -33
  39. aioscrapy/http/response/html.py +42 -3
  40. aioscrapy/http/response/text.py +496 -49
  41. aioscrapy/http/response/web_driver.py +144 -0
  42. aioscrapy/http/response/xml.py +45 -3
  43. aioscrapy/libs/downloader/defaultheaders.py +66 -2
  44. aioscrapy/libs/downloader/downloadtimeout.py +91 -2
  45. aioscrapy/libs/downloader/ja3fingerprint.py +95 -2
  46. aioscrapy/libs/downloader/retry.py +192 -6
  47. aioscrapy/libs/downloader/stats.py +142 -0
  48. aioscrapy/libs/downloader/useragent.py +93 -2
  49. aioscrapy/libs/extensions/closespider.py +166 -4
  50. aioscrapy/libs/extensions/corestats.py +151 -1
  51. aioscrapy/libs/extensions/logstats.py +145 -1
  52. aioscrapy/libs/extensions/metric.py +370 -1
  53. aioscrapy/libs/extensions/throttle.py +235 -1
  54. aioscrapy/libs/pipelines/__init__.py +345 -2
  55. aioscrapy/libs/pipelines/csv.py +242 -0
  56. aioscrapy/libs/pipelines/excel.py +545 -0
  57. aioscrapy/libs/pipelines/mongo.py +132 -0
  58. aioscrapy/libs/pipelines/mysql.py +67 -0
  59. aioscrapy/libs/pipelines/pg.py +67 -0
  60. aioscrapy/libs/spider/depth.py +141 -3
  61. aioscrapy/libs/spider/httperror.py +144 -4
  62. aioscrapy/libs/spider/offsite.py +202 -2
  63. aioscrapy/libs/spider/referer.py +396 -21
  64. aioscrapy/libs/spider/urllength.py +97 -1
  65. aioscrapy/link.py +115 -8
  66. aioscrapy/logformatter.py +199 -8
  67. aioscrapy/middleware/absmanager.py +328 -2
  68. aioscrapy/middleware/downloader.py +218 -0
  69. aioscrapy/middleware/extension.py +50 -1
  70. aioscrapy/middleware/itempipeline.py +96 -0
  71. aioscrapy/middleware/spider.py +360 -7
  72. aioscrapy/process.py +200 -0
  73. aioscrapy/proxy/__init__.py +142 -3
  74. aioscrapy/proxy/redis.py +136 -2
  75. aioscrapy/queue/__init__.py +168 -16
  76. aioscrapy/scrapyd/runner.py +124 -3
  77. aioscrapy/serializer.py +182 -2
  78. aioscrapy/settings/__init__.py +610 -128
  79. aioscrapy/settings/default_settings.py +314 -14
  80. aioscrapy/signalmanager.py +151 -20
  81. aioscrapy/signals.py +183 -1
  82. aioscrapy/spiderloader.py +165 -12
  83. aioscrapy/spiders/__init__.py +233 -6
  84. aioscrapy/statscollectors.py +312 -1
  85. aioscrapy/utils/conf.py +345 -17
  86. aioscrapy/utils/curl.py +168 -16
  87. aioscrapy/utils/decorators.py +76 -6
  88. aioscrapy/utils/deprecate.py +212 -19
  89. aioscrapy/utils/httpobj.py +55 -3
  90. aioscrapy/utils/log.py +79 -0
  91. aioscrapy/utils/misc.py +189 -21
  92. aioscrapy/utils/ossignal.py +67 -5
  93. aioscrapy/utils/project.py +165 -3
  94. aioscrapy/utils/python.py +254 -44
  95. aioscrapy/utils/reqser.py +75 -1
  96. aioscrapy/utils/request.py +173 -12
  97. aioscrapy/utils/response.py +91 -6
  98. aioscrapy/utils/signal.py +196 -14
  99. aioscrapy/utils/spider.py +51 -4
  100. aioscrapy/utils/template.py +93 -6
  101. aioscrapy/utils/tools.py +191 -17
  102. aioscrapy/utils/trackref.py +198 -12
  103. aioscrapy/utils/url.py +341 -36
  104. aio_scrapy-2.1.4.dist-info/METADATA +0 -239
  105. aio_scrapy-2.1.4.dist-info/RECORD +0 -133
  106. aioscrapy/core/downloader/handlers/playwright/__init__.py +0 -115
  107. aioscrapy/core/downloader/handlers/playwright/driverpool.py +0 -59
  108. aioscrapy/core/downloader/handlers/playwright/webdriver.py +0 -96
  109. aioscrapy/http/response/playwright.py +0 -36
  110. aioscrapy/libs/pipelines/execl.py +0 -169
  111. {aio_scrapy-2.1.4.dist-info → aio_scrapy-2.1.7.dist-info}/entry_points.txt +0 -0
  112. {aio_scrapy-2.1.4.dist-info → aio_scrapy-2.1.7.dist-info}/top_level.txt +0 -0
@@ -1,3 +1,14 @@
1
+ """
2
+ RabbitMQ connection manager for aioscrapy.
3
+ aioscrapy的RabbitMQ连接管理器。
4
+
5
+ This module provides classes for managing RabbitMQ connections in aioscrapy.
6
+ It includes a connection manager for creating and managing RabbitMQ connections and channels,
7
+ and an executor for convenient access to RabbitMQ operations.
8
+ 此模块提供了在aioscrapy中管理RabbitMQ连接的类。
9
+ 它包括一个用于创建和管理RabbitMQ连接和通道的连接管理器,以及一个用于方便访问RabbitMQ操作的执行器。
10
+ """
11
+
1
12
  from contextlib import asynccontextmanager
2
13
 
3
14
  import aio_pika
@@ -9,7 +20,28 @@ from aioscrapy.db.absmanager import AbsDBPoolManager
9
20
 
10
21
 
11
22
  class RabbitmqExecutor:
23
+ """
24
+ Executor for RabbitMQ operations.
25
+ RabbitMQ操作的执行器。
26
+
27
+ This class provides a convenient way to execute RabbitMQ operations on a specific
28
+ RabbitMQ connection. It offers methods for publishing messages, getting messages,
29
+ checking queue counts, and cleaning message queues.
30
+ 此类提供了一种在特定RabbitMQ连接上执行RabbitMQ操作的便捷方式。
31
+ 它提供了发布消息、获取消息、检查队列计数和清理消息队列的方法。
32
+ """
33
+
12
34
  def __init__(self, alias: str, pool_manager: "AioRabbitmqManager"):
35
+ """
36
+ Initialize a RabbitmqExecutor.
37
+ 初始化RabbitmqExecutor。
38
+
39
+ Args:
40
+ alias: The alias of the RabbitMQ connection to use.
41
+ 要使用的RabbitMQ连接的别名。
42
+ pool_manager: The RabbitMQ manager that manages the connection.
43
+ 管理连接的RabbitMQ管理器。
44
+ """
13
45
  self.alias = alias
14
46
  self.pool_manager = pool_manager
15
47
 
@@ -22,8 +54,37 @@ class RabbitmqExecutor:
22
54
  timeout=None,
23
55
  **kwargs
24
56
  ):
57
+ """
58
+ Clean (delete) a message queue.
59
+ 清理(删除)消息队列。
60
+
61
+ This method deletes a queue with the specified routing key.
62
+ 此方法删除具有指定路由键的队列。
63
+
64
+ Args:
65
+ routing_key: The routing key (queue name) to delete.
66
+ 要删除的路由键(队列名称)。
67
+ *args: Additional arguments to pass to declare_queue.
68
+ 传递给declare_queue的其他参数。
69
+ if_unused: If True, the queue will only be deleted if it has no consumers.
70
+ 如果为True,则只有当队列没有消费者时才会删除它。
71
+ if_empty: If True, the queue will only be deleted if it has no messages.
72
+ 如果为True,则只有当队列没有消息时才会删除它。
73
+ timeout: Deletion timeout in seconds.
74
+ 删除超时(秒)。
75
+ **kwargs: Additional keyword arguments to pass to declare_queue.
76
+ 传递给declare_queue的其他关键字参数。
77
+
78
+ Returns:
79
+ None
80
+ """
25
81
  async with self.pool_manager.get(self.alias) as channel:
82
+ # Declare the queue to ensure it exists
83
+ # 声明队列以确保它存在
26
84
  queue = await channel.declare_queue(routing_key, *args, **kwargs)
85
+
86
+ # Delete the queue
87
+ # 删除队列
27
88
  await queue.delete(if_unused=if_unused, if_empty=if_empty, timeout=timeout)
28
89
 
29
90
  async def get_message_count(
@@ -32,13 +93,47 @@ class RabbitmqExecutor:
32
93
  *args,
33
94
  **kwargs
34
95
  ):
96
+ """
97
+ Get the number of messages in a queue.
98
+ 获取队列中的消息数量。
99
+
100
+ This method returns the number of messages in a queue with the specified routing key.
101
+ It attempts to get a message from the queue (without removing it) to check the message count.
102
+ 此方法返回具有指定路由键的队列中的消息数量。
103
+ 它尝试从队列中获取一条消息(不删除它)以检查消息计数。
104
+
105
+ Args:
106
+ routing_key: The routing key (queue name) to check.
107
+ 要检查的路由键(队列名称)。
108
+ *args: Additional arguments to pass to declare_queue.
109
+ 传递给declare_queue的其他参数。
110
+ **kwargs: Additional keyword arguments to pass to declare_queue.
111
+ 传递给declare_queue的其他关键字参数。
112
+
113
+ Returns:
114
+ int: The number of messages in the queue, or 0 if the queue is empty.
115
+ 队列中的消息数量,如果队列为空则为0。
116
+ """
35
117
  async with self.pool_manager.get(self.alias) as channel:
118
+ # Declare the queue to ensure it exists
119
+ # 声明队列以确保它存在
36
120
  queue = await channel.declare_queue(routing_key, *args, **kwargs)
121
+
37
122
  try:
123
+ # Try to get a message without acknowledging it
124
+ # 尝试获取一条消息而不确认它
38
125
  result = await queue.get(no_ack=False)
126
+
127
+ # Negative acknowledgment to return the message to the queue
128
+ # 负确认以将消息返回到队列
39
129
  await result.nack()
130
+
131
+ # Return the message count
132
+ # 返回消息计数
40
133
  return result.message_count
41
134
  except QueueEmpty:
135
+ # If the queue is empty, return 0
136
+ # 如果队列为空,则返回0
42
137
  return 0
43
138
 
44
139
  async def get_message(
@@ -47,12 +142,43 @@ class RabbitmqExecutor:
47
142
  *args,
48
143
  **kwargs
49
144
  ):
145
+ """
146
+ Get a message from a queue.
147
+ 从队列中获取消息。
148
+
149
+ This method retrieves and removes a message from a queue with the specified routing key.
150
+ If the queue is empty, it returns None.
151
+ 此方法从具有指定路由键的队列中检索并删除一条消息。
152
+ 如果队列为空,则返回None。
153
+
154
+ Args:
155
+ routing_key: The routing key (queue name) to get a message from.
156
+ 要从中获取消息的路由键(队列名称)。
157
+ *args: Additional arguments to pass to declare_queue.
158
+ 传递给declare_queue的其他参数。
159
+ **kwargs: Additional keyword arguments to pass to declare_queue.
160
+ 传递给declare_queue的其他关键字参数。
161
+
162
+ Returns:
163
+ bytes: The message body, or None if the queue is empty.
164
+ 消息体,如果队列为空则为None。
165
+ """
50
166
  async with self.pool_manager.get(self.alias) as channel:
167
+ # Declare the queue to ensure it exists
168
+ # 声明队列以确保它存在
51
169
  queue = await channel.declare_queue(routing_key, *args, **kwargs)
170
+
52
171
  try:
172
+ # Try to get a message with auto-acknowledgment
173
+ # 尝试获取一条消息并自动确认
53
174
  result = await queue.get(no_ack=True)
175
+
176
+ # Return the message body
177
+ # 返回消息体
54
178
  return result.body
55
179
  except QueueEmpty:
180
+ # If the queue is empty, return None
181
+ # 如果队列为空,则返回None
56
182
  return None
57
183
 
58
184
  async def publish(
@@ -65,9 +191,42 @@ class RabbitmqExecutor:
65
191
  timeout=None,
66
192
  **kwargs
67
193
  ):
194
+ """
195
+ Publish a message to a queue.
196
+ 向队列发布消息。
197
+
198
+ This method publishes a message to a queue with the specified routing key.
199
+ 此方法向具有指定路由键的队列发布消息。
200
+
201
+ Args:
202
+ routing_key: The routing key (queue name) to publish to.
203
+ 要发布到的路由键(队列名称)。
204
+ body: The message body as bytes.
205
+ 消息体(字节)。
206
+ *args: Additional arguments to pass to Message constructor.
207
+ 传递给Message构造函数的其他参数。
208
+ mandatory: If True, the server will return an unroutable message to the client.
209
+ 如果为True,服务器将向客户端返回不可路由的消息。
210
+ immediate: If True, the server will return a message if it cannot be routed to a queue consumer immediately.
211
+ 如果为True,如果消息不能立即路由到队列消费者,服务器将返回消息。
212
+ timeout: Publish timeout in seconds.
213
+ 发布超时(秒)。
214
+ **kwargs: Additional keyword arguments to pass to Message constructor.
215
+ 传递给Message构造函数的其他关键字参数。
216
+
217
+ Returns:
218
+ bool: True if the message was published successfully.
219
+ 如果消息发布成功,则为True。
220
+ """
68
221
  async with self.pool_manager.get(self.alias) as channel:
222
+ # Create a message with the provided body and arguments
223
+ # 使用提供的消息体和参数创建消息
224
+ message = aio_pika.Message(body, *args, **kwargs)
225
+
226
+ # Publish the message to the default exchange with the specified routing key
227
+ # 使用指定的路由键将消息发布到默认交换机
69
228
  return await channel.default_exchange.publish(
70
- aio_pika.Message(body, *args, **kwargs),
229
+ message,
71
230
  routing_key,
72
231
  mandatory=mandatory,
73
232
  immediate=immediate,
@@ -76,96 +235,377 @@ class RabbitmqExecutor:
76
235
 
77
236
 
78
237
  class AioRabbitmqManager(AbsDBPoolManager):
238
+ """
239
+ Manager for RabbitMQ connections.
240
+ RabbitMQ连接的管理器。
241
+
242
+ This class manages RabbitMQ connections and channels. It implements the
243
+ AbsDBPoolManager interface for RabbitMQ connections, providing methods for
244
+ creating, accessing, and closing RabbitMQ connections and channels.
245
+ 此类管理RabbitMQ连接和通道。它为RabbitMQ连接实现了AbsDBPoolManager接口,
246
+ 提供了创建、访问和关闭RabbitMQ连接和通道的方法。
247
+ """
248
+
249
+ # Dictionary to store RabbitMQ connection and channel pools by alias
250
+ # 按别名存储RabbitMQ连接和通道池的字典
79
251
  _clients = {}
80
252
 
81
253
  @staticmethod
82
254
  async def get_channel(connection_pool):
255
+ """
256
+ Get a channel from a connection pool.
257
+ 从连接池获取通道。
258
+
259
+ This static method acquires a connection from the connection pool
260
+ and creates a channel from that connection.
261
+ 此静态方法从连接池获取连接,并从该连接创建通道。
262
+
263
+ Args:
264
+ connection_pool: The RabbitMQ connection pool.
265
+ RabbitMQ连接池。
266
+
267
+ Returns:
268
+ Channel: A RabbitMQ channel.
269
+ RabbitMQ通道。
270
+ """
271
+ # Acquire a connection from the pool
272
+ # 从池中获取连接
83
273
  async with connection_pool.acquire() as connection:
274
+ # Create and return a channel from the connection
275
+ # 从连接创建并返回通道
84
276
  return await connection.channel()
85
277
 
86
278
  async def create(self, alias: str, params: dict):
279
+ """
280
+ Create new RabbitMQ connection and channel pools.
281
+ 创建新的RabbitMQ连接和通道池。
282
+
283
+ This method creates new RabbitMQ connection and channel pools with the given alias and parameters.
284
+ If pools with the given alias already exist, it returns the existing pools.
285
+ 此方法使用给定的别名和参数创建新的RabbitMQ连接和通道池。
286
+ 如果具有给定别名的池已经存在,则返回现有池。
287
+
288
+ Args:
289
+ alias: The alias for the new RabbitMQ pools.
290
+ 新RabbitMQ池的别名。
291
+ params: The parameters for creating the RabbitMQ pools. Must include:
292
+ 创建RabbitMQ池的参数。必须包括:
293
+ - url: RabbitMQ connection URL (required)
294
+ RabbitMQ连接URL(必需)
295
+ - connection_max_size: Maximum number of connections in the pool
296
+ 池中的最大连接数
297
+ - channel_max_size: Maximum number of channels in the pool
298
+ 池中的最大通道数
299
+
300
+ Returns:
301
+ tuple: A tuple containing (connection_pool, channel_pool).
302
+ 包含(连接池, 通道池)的元组。
303
+
304
+ Raises:
305
+ AssertionError: If the URL parameter is missing.
306
+ 如果缺少URL参数。
307
+ """
308
+ # Return existing pools if they exist
309
+ # 如果池已存在,则返回现有池
87
310
  if alias in self._clients:
88
311
  return self._clients[alias]
89
312
 
313
+ # Make a copy of params to avoid modifying the original
314
+ # 复制params以避免修改原始参数
90
315
  params = params.copy()
316
+
317
+ # Extract required URL parameter
318
+ # 提取必需的URL参数
91
319
  url = params.pop('url', None)
92
320
  assert url, "Must args url"
321
+
322
+ # Extract pool size parameters
323
+ # 提取池大小参数
93
324
  connection_max_size = params.pop('connection_max_size', None)
94
325
  channel_max_size = params.pop('channel_max_size', None)
95
326
 
327
+ # Create connection pool
328
+ # 创建连接池
96
329
  connection_pool: Pool = Pool(aio_pika.connect_robust, url, max_size=connection_max_size)
330
+
331
+ # Create channel pool that uses the connection pool
332
+ # 创建使用连接池的通道池
97
333
  channel_pool: Pool = Pool(self.get_channel, connection_pool, max_size=channel_max_size)
98
334
 
335
+ # Store and return the pools
336
+ # 存储并返回池
99
337
  return self._clients.setdefault(alias, (connection_pool, channel_pool))
100
338
 
101
339
  def get_pool(self, alias: str):
102
- connection_pool, channel_pool = self._clients.get(alias)
340
+ """
341
+ Get RabbitMQ connection and channel pools by their alias.
342
+ 通过别名获取RabbitMQ连接和通道池。
343
+
344
+ This method retrieves existing RabbitMQ connection and channel pools with the given alias.
345
+ 此方法检索具有给定别名的现有RabbitMQ连接和通道池。
346
+
347
+ Args:
348
+ alias: The alias of the RabbitMQ pools to retrieve.
349
+ 要检索的RabbitMQ池的别名。
350
+
351
+ Returns:
352
+ tuple: A tuple containing (connection_pool, channel_pool).
353
+ 包含(连接池, 通道池)的元组。
354
+
355
+ Raises:
356
+ AssertionError: If no RabbitMQ pools exist with the given alias.
357
+ 如果不存在具有给定别名的RabbitMQ池。
358
+ """
359
+ # Get the pools from the clients dictionary
360
+ # 从客户端字典获取池
361
+ pools = self._clients.get(alias)
362
+
363
+ if pools:
364
+ connection_pool, channel_pool = pools
365
+ else:
366
+ connection_pool, channel_pool = None, None
367
+
368
+ # Verify that both pools exist
369
+ # 验证两个池都存在
103
370
  assert channel_pool is not None, f"Dont create the rabbitmq channel_pool named {alias}"
104
371
  assert connection_pool is not None, f"Dont create the rabbitmq connection_pool named {alias}"
372
+
105
373
  return connection_pool, channel_pool
106
374
 
107
375
  @asynccontextmanager
108
376
  async def get(self, alias: str):
109
- connection_pool, channel_pool = self.get_pool(alias)
377
+ """
378
+ Get a RabbitMQ channel as an async context manager.
379
+ 获取RabbitMQ通道作为异步上下文管理器。
380
+
381
+ This method provides a convenient way to acquire a channel
382
+ from a RabbitMQ channel pool, and automatically release it when the
383
+ context is exited.
384
+ 此方法提供了一种从RabbitMQ通道池获取通道的便捷方式,
385
+ 并在退出上下文时自动释放它。
386
+
387
+ Example:
388
+ ```python
389
+ async with rabbitmq_manager.get('default') as channel:
390
+ await channel.declare_queue('my_queue')
391
+ ```
392
+
393
+ Args:
394
+ alias: The alias of the RabbitMQ pools to use.
395
+ 要使用的RabbitMQ池的别名。
396
+
397
+ Yields:
398
+ Channel: A RabbitMQ channel.
399
+ RabbitMQ通道。
400
+ """
401
+ # Get the connection and channel pools
402
+ # 获取连接和通道池
403
+ _, channel_pool = self.get_pool(alias)
404
+
405
+ # Acquire a channel from the channel pool
406
+ # 从通道池获取通道
110
407
  async with channel_pool.acquire() as channel:
408
+ # Yield the channel to the caller
409
+ # 将通道传递给调用者
111
410
  yield channel
112
411
 
113
412
  def executor(self, alias: str) -> RabbitmqExecutor:
413
+ """
414
+ Get a RabbitmqExecutor for a specific RabbitMQ connection.
415
+ 获取特定RabbitMQ连接的RabbitmqExecutor。
416
+
417
+ This method creates a RabbitmqExecutor that provides a convenient way to
418
+ execute operations on the RabbitMQ connection with the given alias.
419
+ 此方法创建一个RabbitmqExecutor,提供了一种在具有给定别名的RabbitMQ连接上
420
+ 执行操作的便捷方式。
421
+
422
+ Args:
423
+ alias: The alias of the RabbitMQ connection to use.
424
+ 要使用的RabbitMQ连接的别名。
425
+
426
+ Returns:
427
+ RabbitmqExecutor: An executor for the RabbitMQ connection.
428
+ RabbitMQ连接的执行器。
429
+ """
114
430
  return RabbitmqExecutor(alias, self)
115
431
 
116
432
  async def close(self, alias: str):
433
+ """
434
+ Close specific RabbitMQ connection and channel pools.
435
+ 关闭特定的RabbitMQ连接和通道池。
436
+
437
+ This method closes the RabbitMQ connection and channel pools with the given alias
438
+ and removes them from the managed pools.
439
+ 此方法关闭具有给定别名的RabbitMQ连接和通道池,并将它们从管理的池中移除。
440
+
441
+ Args:
442
+ alias: The alias of the RabbitMQ pools to close.
443
+ 要关闭的RabbitMQ池的别名。
444
+
445
+ Returns:
446
+ None
447
+ """
448
+ # Remove the pools from the managed pools
449
+ # 从管理的池中移除池
117
450
  connection_pool, channel_pool = self._clients.pop(alias, (None, None))
118
451
  connection_pool: Pool
119
452
  channel_pool: Pool
453
+
454
+ # Close the channel pool if it exists
455
+ # 如果通道池存在,则关闭它
120
456
  if channel_pool:
121
457
  await channel_pool.close()
122
458
 
459
+ # Close the connection pool if it exists
460
+ # 如果连接池存在,则关闭它
123
461
  if connection_pool:
124
462
  await connection_pool.close()
125
463
 
126
464
  async def close_all(self):
465
+ """
466
+ Close all RabbitMQ connection and channel pools.
467
+ 关闭所有RabbitMQ连接和通道池。
468
+
469
+ This method closes all RabbitMQ connection and channel pools managed by this manager.
470
+ 此方法关闭此管理器管理的所有RabbitMQ连接和通道池。
471
+
472
+ Returns:
473
+ None
474
+ """
475
+ # Create a copy of the keys to avoid modifying the dictionary during iteration
476
+ # 创建键的副本,以避免在迭代期间修改字典
127
477
  for alias in list(self._clients.keys()):
128
478
  await self.close(alias)
129
479
 
130
480
  async def from_dict(self, db_args: dict):
481
+ """
482
+ Initialize RabbitMQ pools from a configuration dictionary.
483
+ 从配置字典初始化RabbitMQ池。
484
+
485
+ This method creates RabbitMQ connection and channel pools based on the configuration in db_args.
486
+ 此方法根据db_args中的配置创建RabbitMQ连接和通道池。
487
+
488
+ Args:
489
+ db_args: A dictionary mapping aliases to RabbitMQ connection parameters.
490
+ 将别名映射到RabbitMQ连接参数的字典。
491
+ Example:
492
+ {
493
+ 'default': {
494
+ 'url': 'amqp://guest:guest@localhost:5672/',
495
+ 'connection_max_size': 2,
496
+ 'channel_max_size': 10
497
+ },
498
+ 'analytics': {
499
+ 'url': 'amqp://user:password@analytics.example.com:5672/',
500
+ 'connection_max_size': 5,
501
+ 'channel_max_size': 20
502
+ }
503
+ }
504
+
505
+ Returns:
506
+ None
507
+ """
131
508
  for alias, rabbitmq_args in db_args.items():
132
509
  await self.create(alias, rabbitmq_args)
133
510
 
134
511
  async def from_settings(self, settings: aioscrapy.Settings):
512
+ """
513
+ Initialize RabbitMQ pools from aioscrapy settings.
514
+ 从aioscrapy设置初始化RabbitMQ池。
515
+
516
+ This method creates RabbitMQ connection and channel pools based on the RABBITMQ_ARGS setting.
517
+ 此方法根据RABBITMQ_ARGS设置创建RabbitMQ连接和通道池。
518
+
519
+ The RABBITMQ_ARGS setting should be a dictionary mapping aliases to RabbitMQ
520
+ connection parameters, for example:
521
+ RABBITMQ_ARGS设置应该是一个将别名映射到RabbitMQ连接参数的字典,例如:
522
+
523
+ ```python
524
+ RABBITMQ_ARGS = {
525
+ 'default': {
526
+ 'url': 'amqp://guest:guest@localhost:5672/',
527
+ 'connection_max_size': 2,
528
+ 'channel_max_size': 10
529
+ },
530
+ 'analytics': {
531
+ 'url': 'amqp://user:password@analytics.example.com:5672/',
532
+ 'connection_max_size': 5,
533
+ 'channel_max_size': 20
534
+ }
535
+ }
536
+ ```
537
+
538
+ Args:
539
+ settings: The aioscrapy settings object.
540
+ aioscrapy设置对象。
541
+
542
+ Returns:
543
+ None
544
+ """
135
545
  for alias, rabbitmq_args in settings.getdict('RABBITMQ_ARGS').items():
136
546
  await self.create(alias, rabbitmq_args)
137
547
 
138
548
 
549
+ # Singleton instance of AioRabbitmqManager
550
+ # AioRabbitmqManager的单例实例
139
551
  rabbitmq_manager = AioRabbitmqManager()
140
552
 
553
+ # Example usage
554
+ # 示例用法
141
555
  if __name__ == '__main__':
142
556
  import asyncio
143
557
 
144
558
 
145
559
  async def test():
146
- _, channel_pool = await rabbitmq_manager.create('default', {
560
+ """
561
+ Test function demonstrating the usage of the RabbitMQ manager.
562
+ 演示RabbitMQ管理器用法的测试函数。
563
+ """
564
+ # Create RabbitMQ connection and channel pools with alias 'default'
565
+ # 创建别名为'default'的RabbitMQ连接和通道池
566
+ await rabbitmq_manager.create('default', {
147
567
  'url': "amqp://guest:guest@192.168.234.128:5673/",
148
568
  'connection_max_size': 2,
149
569
  'channel_max_size': 10,
150
570
  })
151
571
 
572
+ # Define a queue name for testing
573
+ # 定义用于测试的队列名称
152
574
  queue_name = 'pool_queue'
575
+
576
+ # Get a RabbitMQ executor for the 'default' connection
577
+ # 获取'default'连接的RabbitMQ执行器
153
578
  execute = rabbitmq_manager.executor('default')
154
579
 
580
+ # Publish 10 messages to the queue
581
+ # 向队列发布10条消息
155
582
  for i in range(10):
156
583
  result = await execute.publish(queue_name, f"msg{i}".encode(), priority=3)
157
584
  print(result)
158
585
 
586
+ # Get and print the message count
587
+ # 获取并打印消息计数
159
588
  print(await execute.get_message_count(queue_name))
160
589
 
590
+ # Get and print 5 messages from the queue
591
+ # 从队列获取并打印5条消息
161
592
  for i in range(5):
162
593
  print(await execute.get_message(queue_name))
163
594
  print(await execute.get_message_count(queue_name))
164
595
 
596
+ # Clean the message queue and print the result
597
+ # 清理消息队列并打印结果
165
598
  print(await execute.clean_message_queue(queue_name))
599
+
600
+ # Verify the queue is empty
601
+ # 验证队列为空
166
602
  print(await execute.get_message_count(queue_name))
167
603
 
604
+ # Close all RabbitMQ connections and channels
605
+ # 关闭所有RabbitMQ连接和通道
168
606
  await rabbitmq_manager.close_all()
169
607
 
170
608
 
609
+ # Run the test function
610
+ # 运行测试函数
171
611
  asyncio.get_event_loop().run_until_complete(test())