Skip to main content

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:

  1. systemctl service restart policy: on-failure / always
  2. docker service restart policy: on-failure / always / unless-stopped
  3. 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:

  1. proceed_tasks: count of tasks to proceed for restart
  2. elapsed_time: how many seconds should pass
  3. extra_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.
  4. 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


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

karton_memory_watcher-1.0.2.tar.gz (5.4 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

karton_memory_watcher-1.0.2-py3-none-any.whl (4.8 kB view details)

Uploaded Python 3

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

Hashes for karton_memory_watcher-1.0.2.tar.gz
Algorithm Hash digest
SHA256 241fa1d2fb242c16bcdeb4edf962880e464a42d248db63864f7aee02f22ab41d
MD5 55859e5e7255b397ca1a63c1097474a3
BLAKE2b-256 b47dfc91dfca85b6bd854f452808ce8ebfb7ad9f4827ec4615e4aa1246f75caa

See more details on using hashes here.

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

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file karton_memory_watcher-1.0.2-py3-none-any.whl.

File metadata

File hashes

Hashes for karton_memory_watcher-1.0.2-py3-none-any.whl
Algorithm Hash digest
SHA256 95ee2fb809247112e7e835ee434d1cbfefc1992a59ac22cb33300a957726ce40
MD5 b73f4c746dfa0f93073d7b4d6128ffde
BLAKE2b-256 36c2bc9f36177c5ce80b73f289871c3350c9af8445ce354f0663c7095e963e00

See more details on using hashes here.

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

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page