Skip to main content

Well behaved unix daemons for every occasion.

Project description

Well behaved unix daemons for every occasion.

Project Status

Consider this project to be in maintenance mode.

This project went v1.0.0 in 2014. Since then it has only received two patches that contained code changes. All other patches have been maintenance updates to keep automated testing and static analysis running. I do not expect and am not entertaining new feature asks.

I will still respond to PRs and issues that address bugs or security issues. Please send these to https://github.com/kevinconway/daemons.

What Is Daemons?

Daemons is a resource library for Python developers that want to create daemon processes. The classes in this library provide the basic daemonization, signal handling, and pid management functionality while allowing for any implementation of behaviour and logic.

Example Custom Daemon

import time

from daemons.prefab import run

class SleepyDaemon(run.RunDaemon):

    def run(self):

        while True:

            time.sleep(1)

Now to create a simple init script which can launch the daemon.

#!/usr/bin/env python

import logging
import os
import sys
import time

from mypackage import SleepyDaemon


if __name__ == '__main__':

    action = sys.argv[1]
    logfile = os.path.join(os.getcwd(), "sleepy.log")
    pidfile = os.path.join(os.getcwd(), "sleepy.pid")

    logging.basicConfig(filename=logfile, level=logging.DEBUG)
    d = SleepyDaemon(pidfile=pidfile)

    if action == "start":

        d.start()

    elif action == "stop":

        d.stop()

    elif action == "restart":

        d.restart()

There are more daemon types than the simple RunDaemon. Check the docs for more.

Wrapping Existing Code

Daemons can also be used to daemonize an arbitrary Python function.

import time

from daemons import daemonizer

@daemonizer.run(pidfile="/tmp/sleepy.pid")
def sleepy(sleep_time):

    while True:

        time.sleep(sleep_time)

sleepy(20)  # Daemon started with 20 second sleep time.

The daemonizer also supports adding signal handlers. Check the docs for more.

Daemon Functionality

The daemons in the ‘prefab’ module come bundled with the following features:

  • pidfile management

  • signal handling

  • start/stop/restart functionality

  • unix process daemonization

The default implementation of these feature are all driven by Python standard library modules. Each component may be overridden or extended by adding another base class to your daemon that implements the component interface. Check the ‘interfaces’ package for items to implement.

The ‘prefab’ daemons come in three flavors. The ‘RunDaemon’ requires that you implement the ‘run’ method which should use some form of a loop. If the ‘run’ method completes the process will stop. The ‘StepDaemon’ requires that you implement the ‘step’ method. The process will call ‘step’ on an infinite loop. The eventlet and gevent message daemons require that you implement the ‘get_message’ and ‘handle_message’ methods. These will fetch and handle messages within green-threads.

License

Copyright 2013 Kevin Conway

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

   http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.

Contributing

All contributions to this project are protected under the agreement found in the CONTRIBUTING file. All contributors should read the agreement but, as a summary:

You give us the rights to maintain and distribute your code and we promise
to maintain an open source distribution of anything you contribute.

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

daemons-1.3.1.tar.gz (16.1 kB view details)

Uploaded Source

Built Distributions

daemons-1.3.1-py3-none-any.whl (21.2 kB view details)

Uploaded Python 3

daemons-1.3.1-py2-none-any.whl (21.2 kB view details)

Uploaded Python 2

File details

Details for the file daemons-1.3.1.tar.gz.

File metadata

  • Download URL: daemons-1.3.1.tar.gz
  • Upload date:
  • Size: 16.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.13.0 pkginfo/1.5.0.1 requests/2.18.4 setuptools/41.2.0 requests-toolbelt/0.9.1 tqdm/4.35.0 CPython/3.7.4

File hashes

Hashes for daemons-1.3.1.tar.gz
Algorithm Hash digest
SHA256 49e7a9cad5c7c06f88af95fa0dba299e1c0d3d911f1512b203dd20f5e74b6cea
MD5 f9e13cb04b17e401cbf845fa42071726
BLAKE2b-256 7563b88093e8d62492e93eadd8126c422abe302e6413de7ee5eaab2157233ecd

See more details on using hashes here.

File details

Details for the file daemons-1.3.1-py3-none-any.whl.

File metadata

  • Download URL: daemons-1.3.1-py3-none-any.whl
  • Upload date:
  • Size: 21.2 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.13.0 pkginfo/1.5.0.1 requests/2.18.4 setuptools/41.2.0 requests-toolbelt/0.9.1 tqdm/4.35.0 CPython/3.7.4

File hashes

Hashes for daemons-1.3.1-py3-none-any.whl
Algorithm Hash digest
SHA256 973ab274919ef662fb35cd9103990c1fd672b7b2b62dc3c40ef21e2a98a22f4f
MD5 0618bba96b80e25f51eb085625067a54
BLAKE2b-256 6d75e6913727b5fb46669b95dbe50c1bfbd80cfed6cdad05ae949dcffcb2b0a1

See more details on using hashes here.

File details

Details for the file daemons-1.3.1-py2-none-any.whl.

File metadata

  • Download URL: daemons-1.3.1-py2-none-any.whl
  • Upload date:
  • Size: 21.2 kB
  • Tags: Python 2
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.13.0 pkginfo/1.5.0.1 requests/2.18.4 setuptools/41.2.0 requests-toolbelt/0.9.1 tqdm/4.35.0 CPython/3.7.4

File hashes

Hashes for daemons-1.3.1-py2-none-any.whl
Algorithm Hash digest
SHA256 845c22084661f639c6a4652106cb839bc1cbc66834eebbc97b76fbfe4edd01a2
MD5 f1fff8c79054080fba85039ccd090ea8
BLAKE2b-256 19edddc96943cfa421d370763a470c50387e4d6eaf55deaa417c7151022afdb8

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