human
Complete this python program:
def daemonize(no_close=False, pidfile=None):
assistant
#Here is the Python program you requested.

    """Convert the calling process into a daemon. To make the current Python
    process into a daemon process, you need two lines of code:
    .. python::
    from grizzled.os import daemonize
    daemonize.daemonize()
    If ``daemonize()`` fails for any reason, it throws a ``DaemonError``,
    which is a subclass of the standard ``OSError`` exception. also logs debug
    messages, using the standard Python ``logging`` package, to channel
    "grizzled.os.daemon".
    **Adapted from:** http://software.clapper.org/daemonize/
    **See Also:**
    - Stevens, W. Richard. *Unix Network Programming* (Addison-Wesley, 1990).
    :Parameters:
    no_close : bool
    If ``True``, don\t close the file descriptors. Useful if the
    calling process has already redirected file descriptors to an
    output file. **Warning**: Only set this parameter to ``True`` if
    you\re *sure* there are no open file descriptors to the calling
    terminal. Otherwise, you\ll risk having the daemon re-acquire a
    control terminal, which can cause it to be killed if someone logs
    off that terminal.
    pidfile : str
    Path to file to which to write daemon\s process ID. The string may
    contain a ``${pid}`` token, which is replaced with the process ID
    of the daemon. e.g.: ``/var/run/myserver-${pid}``
    :raise DaemonError: Error during daemonizing
    """
    log = logging.getLogger('grizzled.os.daemon')
    def __fork():
        try:
            return _os.fork()
        except OSError as e:
            raise DaemonError, ('Cannot    fork', e.errno, e.strerror)
    def __redirect_file_descriptors():
        import resource
        maxfd = resource.getrlimit(resource.RLIMIT_NOFILE)[1]
        if (maxfd == resource.RLIM_INFINITY):
            maxfd = MAXFD
        for fd in range(0, maxfd):
            try:
                _os.ttyname(fd)
            except:
                continue
            try:
                _os.close(fd)
            except OSError:
                pass
            _os.open(NULL_DEVICE, _os.O_RDWR)
            _os.dup2(0, 1)
            _os.dup2(0, 2)
    if (_os.name != 'posix'):
        import errno
        raise DaemonError, ('daemonize()    is    only    supported    on    Posix-compliant    systems.', errno.ENOSYS, _os.strerror(errno.ENOSYS))
    try:
        log.debug('Forking    first    child.')
        pid = __fork()
        if (pid != 0):
            _os._exit(0)
        log.debug('Creating    new    session')
        _os.setsid()
        log.debug('Forking    second    child.')
        pid = __fork()
        if (pid != 0):
            _os._exit(0)
        log.debug('Setting    umask')
        _os.umask(UMASK)
        log.debug(('Changing    working    directory    to    "%s"' % WORKDIR))
        _os.chdir(WORKDIR)
        if (not no_close):
            log.debug('Redirecting    file    descriptors')
            __redirect_file_descriptors()
        if pidfile:
            from string import Template
            t = Template(pidfile)
            pidfile = t.safe_substitute(pid=str(_os.getpid()))
            open(pidfile, 'w').write((str(_os.getpid()) + '\n'))
    except DaemonError:
        raise
    except OSError as e:
        raise DaemonError, ('Unable    to    daemonize()', e.errno, e.strerror)