Framework for writing daemons, with API similar to threading and multiprocessing.
Project description
pandaemonium
============
n. the abode of all the daemons [1]_
pandaemonium provides a framework for writing daemons in Python. The API is
based on the threading/multiprocessing models [2]_ [3]_, so the primary way
of creating your own daemon is to either subclass and override the ``run``
method, or provide a function as the ``target`` to the ``Daemon`` class.
Besides ``Daemon`` there is also a locking pid file -- ``PidLockFile``.
``PidLockFile`` can either be used manually, or, if a complete path and file
name are provided to ``Daemon``, used automatically.
usage
=====
Daemon
------
``Daemon(target=None, args=None, kwargs=None, working_directory='/', umask=0,
prevent_core=True, process_ids=None, inherit_files=None,
signal_map=None, stdin=None, stdout=None, stderr=None)``
target
function to call when daemonized
args
positional args to provide to target
kwargs
keyword args to provide to target
chroot
attempt to jail the daemon in this directory
working_directory
directory to change to (relative to chroot, if any)
umask
mask to use when creating files
prevent_core
prevent core dump files from being created
process_ids
tuple of (uid, gid) to switch process to (use (None, None) to disable)
inherit_files
open files or file descriptors to keep open
signal_map
dictionary of signal names or numbers to method names or functions
stdin / stdout / stderr
streams to map the standard streams to (default is ``os.devnull``)
``Daemon.run()``
Method representing the daemon's activity.
You may override this method in a subclass. The standard ``run``
method invokes the callable object passed to the object's constructor as
the target argument, if any, with sequential and keyword arguments taken
from the *args* and *kwargs* arguments, respectively.
``Daemon.start(run=True)``
Start the daemon's activity.
This must be called at most once per daemon object. It arranges for the
object's ``run`` method to be invoked as a daemon process.
If ``run`` is ``False`` then ``start`` enters daemon mode, but returns
just before closing open files and calling ``run``. This is useful if one
needs to do additional setup in the daemon process.
``Daemon.finish_start()``
Only call if ``Daemon.start()`` was called with ``run=False``. Finishes
daemon setup and calls ``run``.
``Daemon context manager``
Instead of calling ``start(run=False)``, doing additional set up, and then
calling ``finish_start()``, one can instead use ``Daemon`` as a context
manager::
daemon = Daemon()
with daemon:
# now in daemon mode, but open files have not been closed nor
# have the std streams been redirected to their final values
do_some_more_setup_as_daemon()
# upon exiting the context manager all non-inherited files are closed
# and the std streams redirected
# nothing here is ever executed
[1] http://dictionary.reference.com/browse/pandemonium
[2] https://docs.python.org/2/library/threading.html#threading.Thread
[3] https://docs.python.org/2/library/multiprocessing.html#multiprocessing.Process
============
n. the abode of all the daemons [1]_
pandaemonium provides a framework for writing daemons in Python. The API is
based on the threading/multiprocessing models [2]_ [3]_, so the primary way
of creating your own daemon is to either subclass and override the ``run``
method, or provide a function as the ``target`` to the ``Daemon`` class.
Besides ``Daemon`` there is also a locking pid file -- ``PidLockFile``.
``PidLockFile`` can either be used manually, or, if a complete path and file
name are provided to ``Daemon``, used automatically.
usage
=====
Daemon
------
``Daemon(target=None, args=None, kwargs=None, working_directory='/', umask=0,
prevent_core=True, process_ids=None, inherit_files=None,
signal_map=None, stdin=None, stdout=None, stderr=None)``
target
function to call when daemonized
args
positional args to provide to target
kwargs
keyword args to provide to target
chroot
attempt to jail the daemon in this directory
working_directory
directory to change to (relative to chroot, if any)
umask
mask to use when creating files
prevent_core
prevent core dump files from being created
process_ids
tuple of (uid, gid) to switch process to (use (None, None) to disable)
inherit_files
open files or file descriptors to keep open
signal_map
dictionary of signal names or numbers to method names or functions
stdin / stdout / stderr
streams to map the standard streams to (default is ``os.devnull``)
``Daemon.run()``
Method representing the daemon's activity.
You may override this method in a subclass. The standard ``run``
method invokes the callable object passed to the object's constructor as
the target argument, if any, with sequential and keyword arguments taken
from the *args* and *kwargs* arguments, respectively.
``Daemon.start(run=True)``
Start the daemon's activity.
This must be called at most once per daemon object. It arranges for the
object's ``run`` method to be invoked as a daemon process.
If ``run`` is ``False`` then ``start`` enters daemon mode, but returns
just before closing open files and calling ``run``. This is useful if one
needs to do additional setup in the daemon process.
``Daemon.finish_start()``
Only call if ``Daemon.start()`` was called with ``run=False``. Finishes
daemon setup and calls ``run``.
``Daemon context manager``
Instead of calling ``start(run=False)``, doing additional set up, and then
calling ``finish_start()``, one can instead use ``Daemon`` as a context
manager::
daemon = Daemon()
with daemon:
# now in daemon mode, but open files have not been closed nor
# have the std streams been redirected to their final values
do_some_more_setup_as_daemon()
# upon exiting the context manager all non-inherited files are closed
# and the std streams redirected
# nothing here is ever executed
[1] http://dictionary.reference.com/browse/pandemonium
[2] https://docs.python.org/2/library/threading.html#threading.Thread
[3] https://docs.python.org/2/library/multiprocessing.html#multiprocessing.Process
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distributions
pandaemonium-0.3.0.zip
(10.2 kB
view details)
pandaemonium-0.3.0.tar.gz
(8.3 kB
view details)
File details
Details for the file pandaemonium-0.3.0.zip
.
File metadata
- Download URL: pandaemonium-0.3.0.zip
- Upload date:
- Size: 10.2 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 14405121e8d415f044d5a096f41981c96d0e3b5d6d63a7363c673854d734e677 |
|
MD5 | d48d6b575fee1c565e22fdcbf730b79b |
|
BLAKE2b-256 | 0f4b7b3328dd1e84ae913b80f9a9057b102a0214e3557093556c186732aa7a4f |
File details
Details for the file pandaemonium-0.3.0.tar.gz
.
File metadata
- Download URL: pandaemonium-0.3.0.tar.gz
- Upload date:
- Size: 8.3 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 6b69edc518a78d55fe7e447a3554063fc94640b5fc033198b66a7fdc651aa4e0 |
|
MD5 | 35139f55fefbcd51d00db8f2f2035f60 |
|
BLAKE2b-256 | 1c75eb1f42c6eb2586a8c1717ecd68a7abf13d0fad844072de51563112e5af5a |