Skip to main content

A simple python package for creating daemon applications.

Project description

daemon-application

A simple python package for creating daemon applications.

Documents

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.

Release

v0.4.4 2021/06/27

  • Fix the problem in sub-command stop.

v0.4.3 2021/06/26

  • Deps on pyyaml.

v0.4.2 2021/06/26

  • Remove a print() statement.

v0.4.1 2021/06/26

  • Fix documents URLs.

v0.4.0 2021/06/26

  • 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 2020/11/22

  • Fix show-config-filepaths.

v0.3.2 2020/11/22

  • Add click deps in requirements.txt

v0.3.1 2020/11/22

  • Add DaemonApplication.

v0.3.0 2020/11/21

  • New wrapper.

v0.2.1 2018/04/18

  • 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.4.4.tar.gz (8.6 kB view details)

Uploaded Source

File details

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

File metadata

  • Download URL: daemon-application-0.4.4.tar.gz
  • Upload date:
  • Size: 8.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.1.1 pkginfo/1.5.0.1 requests/2.23.0 setuptools/41.2.0 requests-toolbelt/0.9.1 tqdm/4.46.0 CPython/3.7.7

File hashes

Hashes for daemon-application-0.4.4.tar.gz
Algorithm Hash digest
SHA256 130b3054e42cb3ed17590fd9297dbff5dbc86c89e5997cc6d6dab18d7ef00028
MD5 e4a7e2d9c795b7e4144ddcadad84478f
BLAKE2b-256 0bc72b3ab51f7b9bd84344eab5033e6ac6a2a3c51017814c00758fb38f9d4ce0

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