Skip to main content

Looper is a daemonizer library, it can help you with lifecycle of your daemon.

Project description

GenesisCoreLibs Looper Documentation

Overview

GCL Looper is a Python library designed to create daemon-like services that can run indefinitely, performing tasks at regular intervals or on demand.

Usage Examples

Basic Service

  • Iterate infinitely
  • There should be at least 5 seconds between start of previous and next iteration (iter_min_period)
  • pause for 1 second between iterations (iter_pause)
from gcl_looper.services import basic

class MyService(basic.BasicService):
    def __init__(self, iter_min_period=5, iter_pause=1):
        super(MyService, self).__init__(iter_min_period, iter_pause)

    def _iteration(self):
        print("Iteration", self._iteration_number)

service = MyService()
service.start()

Finite Service without any pauses in-between

from gcl_looper.services import basic

class MyFiniteService(basic.BasicService):
    def __init__(self, iter_min_period=0, iter_pause=0):
        super(MyFiniteService, self).__init__(iter_min_period, iter_pause)
        self.countdown = 3

    def _iteration(self):
        if self.countdown > 1:
            self.countdown -= 1
        else:
            self.stop()

service = MyFiniteService()
service.start()

API service with database (restalchemy)

from gcl_looper.services import bjoern_service
from gcl_looper.services import hub
from oslo_config import cfg
from restalchemy.storage.sql import engines
from restalchemy.common import config_opts as db_config_opts

from MY_PACKAGE.user_api import app

api_cli_opts = [
    cfg.StrOpt(
        "bind-host", default="127.0.0.1", help="The host IP to bind to"
    ),
    cfg.IntOpt("bind-port", default=8080, help="The port to bind to"),
    cfg.IntOpt(
        "workers", default=1, help="How many http servers should be started"
    ),
]

DOMAIN = "user_api"

CONF = cfg.CONF
CONF.register_cli_opts(api_cli_opts, DOMAIN)
db_config_opts.register_posgresql_db_opts(conf=CONF)


def main():

    serv_hub = hub.ProcessHubService()

    for _ in range(CONF[DOMAIN].workers):
        service = bjoern_service.BjoernService(
            wsgi_app=app.build_wsgi_application(),
            host=CONF[DOMAIN].bind_host,
            port=CONF[DOMAIN].bind_port,
            bjoern_kwargs=dict(reuse_port=True),
        )

        service.add_setup(
            lambda: engines.engine_factory.configure_postgresql_factory(
                conf=CONF
            )
        )

        serv_hub.add_service(service)

    serv_hub.start()


if __name__ == "__main__":
    main()

Public interface:

  • start(): Starts the service.
  • stop(): Stop the service.
  • _loop_iteration(): Performs one iteration of the service loop.

Implement these methods to get usable service:

  • _iteration(): This method must be implemented by subclasses to perform the actual work at each iteration.

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

gcl_looper-1.0.0.tar.gz (14.6 kB view details)

Uploaded Source

Built Distribution

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

gcl_looper-1.0.0-py3-none-any.whl (18.3 kB view details)

Uploaded Python 3

File details

Details for the file gcl_looper-1.0.0.tar.gz.

File metadata

  • Download URL: gcl_looper-1.0.0.tar.gz
  • Upload date:
  • Size: 14.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.12.9

File hashes

Hashes for gcl_looper-1.0.0.tar.gz
Algorithm Hash digest
SHA256 94da2e836ea6197e76691fba61217a4e6cbc4f9b526f9de899b319a59408b012
MD5 2aacf6c544cdeea00b6368be95133191
BLAKE2b-256 f0e82be31ab16a4c3e2718c8dcbe0a500c7ff63c4727695de576c6ea5cd19e6e

See more details on using hashes here.

Provenance

The following attestation bundles were made for gcl_looper-1.0.0.tar.gz:

Publisher: publish-to-pypi.yml on infraguys/gcl_looper

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

File details

Details for the file gcl_looper-1.0.0-py3-none-any.whl.

File metadata

  • Download URL: gcl_looper-1.0.0-py3-none-any.whl
  • Upload date:
  • Size: 18.3 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.12.9

File hashes

Hashes for gcl_looper-1.0.0-py3-none-any.whl
Algorithm Hash digest
SHA256 0f4ed4accafef767947c716b27c5db53e35d78824e4d3422a942f04421279416
MD5 0d860f5da40db040f9ad8523021b8baf
BLAKE2b-256 4ccce6bb9bfe74a66b0bee88ab18c5a7fc3aed385230efe39f549fd438269062

See more details on using hashes here.

Provenance

The following attestation bundles were made for gcl_looper-1.0.0-py3-none-any.whl:

Publisher: publish-to-pypi.yml on infraguys/gcl_looper

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