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.1.03.zip
(9.7 kB
view details)
pandaemonium-0.1.03.tar.gz
(7.9 kB
view details)
File details
Details for the file pandaemonium-0.1.03.zip
.
File metadata
- Download URL: pandaemonium-0.1.03.zip
- Upload date:
- Size: 9.7 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | c9d405a001d4facc842172d4abab59620161a602802aac6df822a39c868f2f72 |
|
MD5 | 88754ce550c5280ae5c7adc945578cf9 |
|
BLAKE2b-256 | 1526b7b36a0cd33db0cffcdbe836318bd2d086899167ad1ac3b9000d0545b29c |
File details
Details for the file pandaemonium-0.1.03.tar.gz
.
File metadata
- Download URL: pandaemonium-0.1.03.tar.gz
- Upload date:
- Size: 7.9 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 8fb9626e3b4bc1357291d09f53f8add4cb200c1378b759998be22bdabf78aa50 |
|
MD5 | f17ca235cc18e502f771f71d8f6a59a2 |
|
BLAKE2b-256 | b271a55962b54b9d8b8a2ab2353f4271431b6ace7e631e49bbd7ca568a87617c |