Memory watcher for karton consumers
Project description
karton memory watcher
Working for several months in a row, tasks gradually become more and more significant in terms of RAM consumption. And sometimes forcing a call to the garbage collector doesn't help. 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 is a watcher that monitors the gradual increase in the service's RAM usage and shuts it upon reaching a set threshold.
For what ways to run karton consumer is it suitable?
For every way that uses autorestart. 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:
python3 -m 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, )
)
)
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.
Alternatives?
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
wip, feel free to suggest how to gently restart container to allow task normally proceed
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
Hashes for karton_memory_watcher-1.0.1.tar.gz
Algorithm | Hash digest | |
---|---|---|
SHA256 | 2708b0f7735e4cdbfb459feed7adefc4b5493d4995c649a0841caded8ce8e04a |
|
MD5 | eae389b43f33192e3f45367919c91fc4 |
|
BLAKE2b-256 | aa04c28df65b5d30dd422013ad55ee47d66310743bed695e3c1e279bf22b85f7 |
Hashes for karton_memory_watcher-1.0.1-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 86d741d059a8ee84db03eda8b72e1b159144f27eea148fb1530968022db7d78b |
|
MD5 | 9fa83ae10196b773743acb7c8109418e |
|
BLAKE2b-256 | 3464677b77020da458344ee5dcc42a0e72bcf7a500abe843807717d455ab408c |