karton memory watcher
This is a tool for karton workers.
Working for several months in a row, Python processes consumes more and more RAM. And sometimes calls for GC are not a solution. This is especially felt when working with libraries that help to parse certain file formats - at the start, the process takes up 60 megabytes of RAM, and after a couple of months - 400.
This library implements a watcher that monitors the gradual increase in the service's RAM usage and shuts it upon reaching a set threshold.
Cases to run karton-memory-watcher is suitable
Every way that can auto-restart your service. For example, I can suggest at least three of them:
- systemctl service restart policy:
on-failure/always - docker service restart policy:
on-failure/always/unless-stopped - Screen infinite loop:
while true; do your_app; sleep 10; done
Installation
You can install it via pip:
pip install karton-memory-watcher
How to use it
Simple way:
from karton.core import Consumer
from karton.memory_watcher import implant_watcher, RestartBehavior, RestartRule
class YourConsumer(Consumer):
pass
if __name__ == "__main__":
foobar = YourConsumer()
implant_watcher(
foobar,
RestartRule(
extra_consumed_memory_percent=80,
# call_before_exit=(close_db_connections, )
# restart_behavior=RestartBehavior.EXIT_0
)
)
foobar.loop()
RestartRule modes:
proceed_tasks: count of tasks to proceed for restartelapsed_time: how many seconds should passextra_consumed_memory_percent: extra memory used in % (100% means twice of memory compared to point before starting first task). It uses megabytes to calculate percents.extra_consumed_megabytes: extra memory used in megabytes (e.g. your service at start uses 60MB and you need to kill it if it consumes extra 500+ MB)
You can see usage cases in tests.
Usage for Producer
It's a bit harder, because karton-memory-watcher relies on hooks that are specific for Karton.core.Consumer and Karton.core.Karton.
To use on Karton.core.Producer, you need to register hooks manually:
from karton.core import Producer
from karton.memory_watcher import implant_watcher, RestartBehavior, RestartRule
class YourProducer(Producer):
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self.watcher = RestartRule(
extra_consumed_memory_percent=80,
)
self.watcher.pre_hook_behavior()
...
def your_job(self):
while True:
...
# after processing your work and saving all states required
self.watcher.post_hook_behavior()
if __name__ == "__main__":
foobar = YourProducer()
foobar.your_job()
Alternatives ways for restart policies
Systemctl:
In January 2023 there is a nice feature request about MemoryMax policy: https://github.com/systemd/systemd/issues/25966
Also writing your own agent that will track memory leaks and send systemctl service to restart (but be careful not to kill ongoing tasks!).
Docker
AFAIK docker does not have a capability to send SIGINT to container on 'sort' memory limit.
You can implement your own host watcher that will use docker stats --no-stream --format "{{.MemUsage}}" $CONTAINER_NAME and docker kill --signal=SIGINT $CONTAINER_NAME.
Metadata
Release files for karton-memory-watcher 1.0.2
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| karton_memory_watcher-1.0.2.tar.gz | 5.4 kB | With provenance | Details for karton_memory_watcher-1.0.2.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| karton_memory_watcher-1.0.2-py3-none-any.whl | Python 3 | none | any | Details for karton_memory_watcher-1.0.2-py3-none-any.whl |
Total release size: 10.2 kB
Release files / karton_memory_watcher-1.0.2.tar.gz
| Download URL | karton_memory_watcher-1.0.2.tar.gz |
|---|---|
| Size | 5.4 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
241fa1d2fb242c16bcdeb4edf962880e464a42d248db63864f7aee02f22ab41d
|
|
BLAKE2b-256 checksum How to use checksums |
b47dfc91dfca85b6bd854f452808ce8ebfb7ad9f4827ec4615e4aa1246f75caa
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/6.0.1 CPython/3.12.8
|
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
PyPI Publish Attestation
PyPI verified that this artifact, at this checksum, originated from the publisher listed below.
Signed by GitHub Actions, verified by PyPI on Jan 20, 2025.
Transparency logRelease files / karton_memory_watcher-1.0.2-py3-none-any.whl
| Download URL | karton_memory_watcher-1.0.2-py3-none-any.whl |
|---|---|
| Size | 4.8 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
95ee2fb809247112e7e835ee434d1cbfefc1992a59ac22cb33300a957726ce40
|
|
BLAKE2b-256 checksum How to use checksums |
36c2bc9f36177c5ce80b73f289871c3350c9af8445ce354f0663c7095e963e00
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/6.0.1 CPython/3.12.8
|
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
PyPI Publish Attestation
PyPI verified that this artifact, at this checksum, originated from the publisher listed below.
Signed by GitHub Actions, verified by PyPI on Jan 20, 2025.
Transparency logSecurity for karton-memory-watcher 1.0.2
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
Full provenance
All 2 files in this release have been uploaded with provenance metadata.
Attestation for karton_memory_watcher-1.0.2.tar.gz
PyPI Publish Attestation
PyPI verified that this artifact, at this checksum, originated from the publisher listed below.
Signed by GitHub Actions, verified by PyPI on Jan 20, 2025.
Transparency logA note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.