zenx 0.6.2__py3-none-any.whl → 0.6.3__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.
- zenx/engine.py +20 -0
- zenx/spiders/base.py +18 -0
- {zenx-0.6.2.dist-info → zenx-0.6.3.dist-info}/METADATA +1 -1
- {zenx-0.6.2.dist-info → zenx-0.6.3.dist-info}/RECORD +7 -7
- {zenx-0.6.2.dist-info → zenx-0.6.3.dist-info}/WHEEL +0 -0
- {zenx-0.6.2.dist-info → zenx-0.6.3.dist-info}/entry_points.txt +0 -0
- {zenx-0.6.2.dist-info → zenx-0.6.3.dist-info}/top_level.txt +0 -0
zenx/engine.py
CHANGED
@@ -26,6 +26,16 @@ class Engine:
|
|
26
26
|
self.shutdown_event.set()
|
27
27
|
|
28
28
|
|
29
|
+
async def _log_stats(self, spider: Spider) -> None:
|
30
|
+
while True:
|
31
|
+
await asyncio.sleep(60)
|
32
|
+
try:
|
33
|
+
rpm = spider.get_stats().get("rpm")
|
34
|
+
spider.logger.info("stats", rpm=rpm)
|
35
|
+
except asyncio.CancelledError:
|
36
|
+
break
|
37
|
+
|
38
|
+
|
29
39
|
async def _execute(self, spider_name: str) -> None:
|
30
40
|
loop = asyncio.get_running_loop()
|
31
41
|
loop.add_signal_handler(signal.SIGINT, self._shutdown_handler)
|
@@ -48,6 +58,7 @@ class Engine:
|
|
48
58
|
|
49
59
|
spider = spider_cls(client=client, pm=pm, logger=logger, settings=settings)
|
50
60
|
|
61
|
+
stats_task = asyncio.create_task(self._log_stats(spider))
|
51
62
|
try:
|
52
63
|
if self.forever:
|
53
64
|
while not self.shutdown_event.is_set():
|
@@ -61,6 +72,15 @@ class Engine:
|
|
61
72
|
finally:
|
62
73
|
if self.shutdown_event.is_set():
|
63
74
|
logger.info("shutdown", spider=spider_name)
|
75
|
+
|
76
|
+
stats_task.cancel()
|
77
|
+
try:
|
78
|
+
await stats_task
|
79
|
+
except asyncio.CancelledError:
|
80
|
+
pass
|
81
|
+
stats = spider.get_stats()
|
82
|
+
logger.info("summary", **stats)
|
83
|
+
|
64
84
|
await client.close()
|
65
85
|
await db.close()
|
66
86
|
await pm.close_pipelines()
|
zenx/spiders/base.py
CHANGED
@@ -1,5 +1,7 @@
|
|
1
1
|
from __future__ import annotations
|
2
2
|
from abc import ABC, abstractmethod
|
3
|
+
from collections import deque
|
4
|
+
import time
|
3
5
|
from typing import ClassVar, Dict, List, Literal, Type
|
4
6
|
from structlog import BoundLogger
|
5
7
|
|
@@ -44,6 +46,22 @@ class Spider(ABC):
|
|
44
46
|
self.pm = pm
|
45
47
|
self.logger = logger
|
46
48
|
self.settings = settings
|
49
|
+
# stats
|
50
|
+
self.requests_timestamps = deque()
|
51
|
+
self.total_requests = 0
|
52
|
+
|
53
|
+
|
54
|
+
def get_stats(self) -> Dict:
|
55
|
+
# calculate RPM based on rolling window of 1 min
|
56
|
+
now = time.time()
|
57
|
+
# remove requests older than 60 sec
|
58
|
+
while self.requests_timestamps and self.requests_timestamps[0] < (now - 60):
|
59
|
+
self.requests_timestamps.popleft()
|
60
|
+
rpm = len(self.requests_timestamps)
|
61
|
+
return {
|
62
|
+
"rpm": rpm,
|
63
|
+
"total_requests": self.total_requests,
|
64
|
+
}
|
47
65
|
|
48
66
|
|
49
67
|
@abstractmethod
|
@@ -1,7 +1,7 @@
|
|
1
1
|
zenx/cli.py,sha256=pHKhOTdqI6NQQoYK91waRIMpxCXLYtXEryzVaTbmvqc,2810
|
2
2
|
zenx/debug_runner.py,sha256=B2Jd9A4_EHDa-ohLcwnFIxOV73FagTWXX2fl3qgwlpY,809
|
3
3
|
zenx/discovery.py,sha256=YANVGzy2IG1fYruUud-11Y-ynyO6iEp3EjlHnhIQJQI,1014
|
4
|
-
zenx/engine.py,sha256=
|
4
|
+
zenx/engine.py,sha256=1rIX44wMjgGrvarVVe6uD3MQO9HJ31cBHSTPqo-qNE8,2948
|
5
5
|
zenx/exceptions.py,sha256=BJXxzwwX2CU6inhppfblx8c8Z6Mhvsk7MAhQ1LAnhBg,37
|
6
6
|
zenx/logger.py,sha256=UmEk0vV1mSCozV7z_DDgCCXdAManDr5wgkrhKiRQtyU,1651
|
7
7
|
zenx/settings.py,sha256=Q0z3oGyVkhucRyqsraZQR1lAPSq1sjiGKmAkDCmQsA0,940
|
@@ -19,9 +19,9 @@ zenx/resources/proto/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hS
|
|
19
19
|
zenx/resources/proto/feed_pb2.py,sha256=ZyICOLnyuXekkvV4bAHZ1nE1-wwzcYYRRrmRJCMrSoo,2810
|
20
20
|
zenx/resources/proto/feed_pb2_grpc.py,sha256=Mim6FfBgIMj0PmTqHk036nVUMJH3A6I3ts6r1j3bQF8,7441
|
21
21
|
zenx/spiders/__init__.py,sha256=rs5LuqdM2MQlUYiTGJrzkYhzN8_SSLTrR7wGjSRrrSo,25
|
22
|
-
zenx/spiders/base.py,sha256=
|
23
|
-
zenx-0.6.
|
24
|
-
zenx-0.6.
|
25
|
-
zenx-0.6.
|
26
|
-
zenx-0.6.
|
27
|
-
zenx-0.6.
|
22
|
+
zenx/spiders/base.py,sha256=LaX29ArNIAiv8E8hUUpJxahBpRnGfskDlxx4hU0gsTg,2232
|
23
|
+
zenx-0.6.3.dist-info/METADATA,sha256=eoDHZHHXDTZwAkOx8yovY5VIJnjHV2XJcSHoft8stUg,1273
|
24
|
+
zenx-0.6.3.dist-info/WHEEL,sha256=_zCd3N1l69ArxyTb8rzEoP9TpbYXkqRFSNOD5OuxnTs,91
|
25
|
+
zenx-0.6.3.dist-info/entry_points.txt,sha256=8JXob2f1VtvzGFris-e9Usqywg7oca-cChDlH9moOZU,38
|
26
|
+
zenx-0.6.3.dist-info/top_level.txt,sha256=JeXwvK86d7sB-2x-avugFnZIZa33zaHWKI8RHWJR6KY,5
|
27
|
+
zenx-0.6.3.dist-info/RECORD,,
|
File without changes
|
File without changes
|
File without changes
|