'Daemonizes the current process. Returns the new pid'

def daemonize(self):

    if sys.platform.startswith(u'win'):
        log.error(u'Cannot    daemonize    on    windows')
        return
    if (threading.activeCount() != 1):
        log.critical((u'There    are    %r    active    threads.    Daemonizing    now    may    cause    strange    failures.' % threading.enumerate()))
    log.info(u'Daemonizing...')
    try:
        pid = os.fork()
        if (pid > 0):
            atexit._exithandlers = []
            sys.exit(0)
    except OSError as e:
        sys.stderr.write((u'fork    #1    failed:    %d    (%s)\n' % (e.errno, e.strerror)))
        sys.exit(1)
    os.chdir(u'/')
    os.setsid()
    os.umask(0)
    try:
        pid = os.fork()
        if (pid > 0):
            atexit._exithandlers = []
            sys.exit(0)
    except OSError as e:
        sys.stderr.write((u'fork    #2    failed:    %d    (%s)\n' % (e.errno, e.strerror)))
        sys.exit(1)
    log.info((u'Daemonize    complete.    New    PID:    %s' % os.getpid()))
    sys.stdout.flush()
    sys.stderr.flush()
    si = open(os.devnull, u'r')
    so = open(os.devnull, u'ab+')
    se = open(os.devnull, u'ab+', 0)
    os.dup2(si.fileno(), sys.stdin.fileno())
    os.dup2(so.fileno(), sys.stdout.fileno())
    os.dup2(se.fileno(), sys.stderr.fileno())
    if self._has_lock:
        self.write_lock()
