Memory watcher for karton consumers
Project description
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.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file karton_memory_watcher-1.0.2.tar.gz.
File metadata
- Download URL: karton_memory_watcher-1.0.2.tar.gz
- Upload date:
- Size: 5.4 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.0.1 CPython/3.12.8
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
241fa1d2fb242c16bcdeb4edf962880e464a42d248db63864f7aee02f22ab41d
|
|
| MD5 |
55859e5e7255b397ca1a63c1097474a3
|
|
| BLAKE2b-256 |
b47dfc91dfca85b6bd854f452808ce8ebfb7ad9f4827ec4615e4aa1246f75caa
|
Provenance
The following attestation bundles were made for karton_memory_watcher-1.0.2.tar.gz:
Publisher:
pypi-workflow.yml on rakovskij-stanislav/karton_memory_watcher
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
karton_memory_watcher-1.0.2.tar.gz -
Subject digest:
241fa1d2fb242c16bcdeb4edf962880e464a42d248db63864f7aee02f22ab41d - Sigstore transparency entry: 163852247
- Sigstore integration time:
-
Permalink:
rakovskij-stanislav/karton_memory_watcher@b777aae9d762afdc6db77276a8bbed7d6289b640 -
Branch / Tag:
refs/heads/main - Owner: https://github.com/rakovskij-stanislav
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
pypi-workflow.yml@b777aae9d762afdc6db77276a8bbed7d6289b640 -
Trigger Event:
push
-
Statement type:
File details
Details for the file karton_memory_watcher-1.0.2-py3-none-any.whl.
File metadata
- Download URL: karton_memory_watcher-1.0.2-py3-none-any.whl
- Upload date:
- Size: 4.8 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.0.1 CPython/3.12.8
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
95ee2fb809247112e7e835ee434d1cbfefc1992a59ac22cb33300a957726ce40
|
|
| MD5 |
b73f4c746dfa0f93073d7b4d6128ffde
|
|
| BLAKE2b-256 |
36c2bc9f36177c5ce80b73f289871c3350c9af8445ce354f0663c7095e963e00
|
Provenance
The following attestation bundles were made for karton_memory_watcher-1.0.2-py3-none-any.whl:
Publisher:
pypi-workflow.yml on rakovskij-stanislav/karton_memory_watcher
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
karton_memory_watcher-1.0.2-py3-none-any.whl -
Subject digest:
95ee2fb809247112e7e835ee434d1cbfefc1992a59ac22cb33300a957726ce40 - Sigstore transparency entry: 163852249
- Sigstore integration time:
-
Permalink:
rakovskij-stanislav/karton_memory_watcher@b777aae9d762afdc6db77276a8bbed7d6289b640 -
Branch / Tag:
refs/heads/main - Owner: https://github.com/rakovskij-stanislav
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
pypi-workflow.yml@b777aae9d762afdc6db77276a8bbed7d6289b640 -
Trigger Event:
push
-
Statement type: