Skip to main content

A simple python package for creating daemon applications.

Project description

daemon-application

Description

A simple python package for creating daemon applications.

Notice:

  • Runs the application in daemon mode on Linux only. On Windows, the application runs in foreground model.

Install

pip install daemon-application

Usage

Example for raw APIs

import time
import threading
import signal
from daemon_application import daemon_start

stopflag = False

def main():
    def on_exit(*args, **kwargs):
        with open("backgroud.log", "a", encoding="utf-8") as fobj:
            print("process got exit signal...", file=fobj)
            print(args, file=fobj)
            print(kwargs, file=fobj)
        global stopflag
        stopflag = True
    signal.signal(signal.SIGTERM, on_exit)
    signal.signal(signal.SIGINT, on_exit)
    while not stopflag:
        time.sleep(1)
        print(time.time())

if __name__ == "__main__":
    print("start background application...")
    daemon_start(main, "background.pid", True)

Example for DaemonApplication

import time
from daemon_application import DaemonApplication

class HelloApplication(DaemonApplication):
    def main(self):
        while True:
            print("hello")
            time.sleep(1)

controller = HelloApplication().get_controller()

if __name__ == "__main__":
    controller()

Example for DaemonApplication adding new global options

import time
import click
from daemon_application import DaemonApplication

class HelloApplication(DaemonApplication):

    def get_main_options(self):
        options = [
            click.option("-m", "--message", default="hello")
        ]
        return options + super().get_main_options()

    def main(self):
        while True:
            print(self.config["message"])
            time.sleep(1)

controller = HelloApplication().get_controller()

if __name__ == "__main__":
    controller()

The output of the command help that added a new global option

Usage: example.py [OPTIONS] COMMAND [ARGS]...

Options:
  --pidfile TEXT          pidfile file path.
  --workspace TEXT        Set running folder
  --daemon / --no-daemon  Run application in background or in foreground.
  -c, --config TEXT       Config file path. Application will search config
                          file if this option is missing. Use sub-command
                          show-config-fileapaths to get the searching tactics.

  -m, --message TEXT
  --help                  Show this message and exit.

Commands:
  restart                Restart Daemon application.
  show-config-filepaths  Print out the config searching paths.
  start                  Start daemon application.
  stop                   Stop daemon application.

Configs

Config items and default values

  • pidfile: app.pid
  • stop-timeout: 30
  • stop-signal: SIGINT
  • daemon: True
  • workspace: ""
  • loglevel: INFO
  • logfile: app.log
  • logfmt: default

srpcd config items and default values

  • server.listen: 0.0.0.0
  • server.port: 8381
  • server.engine_class: daemon_application.app.SimpleXmlRpcServerEngine
  • enable-debug-service: true
  • services: []

services fields

  • class: class path string, e.g. zenutils.serviceutils.DebugService
  • args: []
  • kwargs: {}

Note

Logging is not enabled by default, you may need to enable it at the first line of the main function using logutils.setup(**self.config).

Release

v0.5.10

  • Use dictutils.deep_merge to update config.

v0.5.9

  • Unit test passed.

v0.5.8

  • Work with zenutils.socketserverutils.

v0.5.7

  • Improve the srpcd command.

v0.5.6

  • Add SimpleRpcServer class and srpcd command.

v0.5.5

  • Config in DaemonApplication is set to the dictutils.Object class for ease use while remaining compatible with all dict operations.

v0.5.4

  • Doc update.

v0.5.3

  • Add DaemonApplication.load_config, so that you can start DaemonApplication service directly by your code.
  • Add stop_timeout for daemon_stop. If stop timeout, kill process tree by force.

v0.5.2

  • Add global options: loglevel, logfile, logfmt.
  • Update default_config override mechanism.

v0.4.4

  • Fix the problem in sub-command stop.

v0.4.3

  • Deps on pyyaml.

v0.4.2

  • Remove a print() statement.

v0.4.1

  • Fix documents URLs.

v0.4.0

  • Remove fastutils deps.
  • Add --config global command option for DaemonApplication.
  • Provide a way to override the global options for subclass of DaemonApplication.
  • The sub-command restart will do just start if the old application is not running or crashed.
  • Use gitee.com source code hosting service.

v0.3.3

  • Fix show-config-filepaths.

v0.3.2

  • Add click deps in requirements.txt

v0.3.1

  • Add DaemonApplication.

v0.3.0

  • New wrapper.

v0.2.1

  • Old releases.

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

daemon-application-0.5.10.tar.gz (11.7 kB view details)

Uploaded Source

Built Distribution

daemon_application-0.5.10-py3-none-any.whl (10.3 kB view details)

Uploaded Python 3

File details

Details for the file daemon-application-0.5.10.tar.gz.

File metadata

  • Download URL: daemon-application-0.5.10.tar.gz
  • Upload date:
  • Size: 11.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.1 CPython/3.10.4

File hashes

Hashes for daemon-application-0.5.10.tar.gz
Algorithm Hash digest
SHA256 d4f86cb56378704737ea34e8d64cbc80e2d63e9a4c47465e4cabcbc1e38f5be3
MD5 663cb13ff89023ef2dc938e419ac86bc
BLAKE2b-256 8d28324ade655eb9009f336e3dc973f60638d0b535952b01836ba5e02184e077

See more details on using hashes here.

File details

Details for the file daemon_application-0.5.10-py3-none-any.whl.

File metadata

File hashes

Hashes for daemon_application-0.5.10-py3-none-any.whl
Algorithm Hash digest
SHA256 417ddc74a43f86b00bf6d01a7f05154068d498903051a359a4d53f867ff5d70b
MD5 9466e5990d916451bfe7d686cf4ac4d4
BLAKE2b-256 a000a8acb800012745840841be6304794d757ffd486966dde9316f9bf3470136

See more details on using hashes here.

Supported by

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