New version: 2.1.
Now we can optionally keep some FDs open.
This commit is contained in:
@@ -23,3 +23,6 @@ You can install it from Python Package Index (PyPI):
|
|||||||
|
|
||||||
daemon = Daemonize(app="test_app", pid=pid, action=main)
|
daemon = Daemonize(app="test_app", pid=pid, action=main)
|
||||||
daemon.start()
|
daemon.start()
|
||||||
|
|
||||||
|
## File descriptors
|
||||||
|
Daemonize object's constructor understands the optional argument **keep_fds** which can contain a list of fds which should remain open. TODO: explain it more.
|
||||||
|
|||||||
@@ -16,11 +16,14 @@ class Daemonize(object):
|
|||||||
- app: contains the application name which will be sent to syslog
|
- app: contains the application name which will be sent to syslog
|
||||||
- pid: path to the pidfile
|
- pid: path to the pidfile
|
||||||
- action: your custom function which will be executed after daemonization.
|
- action: your custom function which will be executed after daemonization.
|
||||||
|
- keep_fds: optional list of fds which should remain open
|
||||||
"""
|
"""
|
||||||
def __init__(self, app, pid, action):
|
def __init__(self, app, pid, action, keep_fds=None):
|
||||||
self.app = app
|
self.app = app
|
||||||
self.pid = pid
|
self.pid = pid
|
||||||
self.action = action
|
self.action = action
|
||||||
|
if keep_fds:
|
||||||
|
self.keep_fds = keep_fds
|
||||||
# Initialize logging.
|
# Initialize logging.
|
||||||
self.logger = logging.getLogger(self.app)
|
self.logger = logging.getLogger(self.app)
|
||||||
self.logger.setLevel(logging.DEBUG)
|
self.logger.setLevel(logging.DEBUG)
|
||||||
@@ -72,7 +75,8 @@ class Daemonize(object):
|
|||||||
# Uh oh, there was a problem.
|
# Uh oh, there was a problem.
|
||||||
sys.exit(1)
|
sys.exit(1)
|
||||||
|
|
||||||
# Close file descriptors
|
# Close all file descriptors, except the ones mentioned in
|
||||||
|
# self.keep_fds.
|
||||||
devnull = "/dev/null"
|
devnull = "/dev/null"
|
||||||
if hasattr(os, "devnull"):
|
if hasattr(os, "devnull"):
|
||||||
# Python has set os.devnull on this system, use it instead
|
# Python has set os.devnull on this system, use it instead
|
||||||
@@ -80,6 +84,7 @@ class Daemonize(object):
|
|||||||
devnull = os.devnull
|
devnull = os.devnull
|
||||||
|
|
||||||
for fd in range(resource.getrlimit(resource.RLIMIT_NOFILE)[0]):
|
for fd in range(resource.getrlimit(resource.RLIMIT_NOFILE)[0]):
|
||||||
|
if fd not in self.keep_fds:
|
||||||
try:
|
try:
|
||||||
os.close(fd)
|
os.close(fd)
|
||||||
except OSError:
|
except OSError:
|
||||||
|
|||||||
2
setup.py
2
setup.py
@@ -4,7 +4,7 @@ from setuptools import setup, find_packages
|
|||||||
|
|
||||||
setup(
|
setup(
|
||||||
name="daemonize",
|
name="daemonize",
|
||||||
version="2.0",
|
version="2.1",
|
||||||
py_modules=["daemonize"],
|
py_modules=["daemonize"],
|
||||||
author="Ilya A. Otyutskiy",
|
author="Ilya A. Otyutskiy",
|
||||||
author_email="sharp@thesharp.ru",
|
author_email="sharp@thesharp.ru",
|
||||||
|
|||||||
Reference in New Issue
Block a user