code: def daemonize(pid_file, working_dir, func):
      
  
      def _fork():
          """ Fork a child process.
  
              Returns ``False`` if fork failed; otherwise,
              we are inside the new child process.
              """
          try:
              pid = os.fork()
              if pid > 0:
                  os._exit(0)  # exit parent
              return True
  
          except OSError:
              return False
  
      def _register_pidfile(filename):
          """ Registers a pid file for the current process which will cleaned up
              when the process terminates.
  
              `filename`
                  Filename to save pid to.
              """
          if common.writefile(filename, str(os.getpid()) + os.linesep):
              os.chmod(filename, 0644)  # rw-r--r--
  
              def _cleanup_pid():
                  """ Removes pidfile.
                      """
                  common.safe_remove_file(filename)
              atexit.register(_cleanup_pid)
  
      if not pid_file or not working_dir or not func:
          return
  
      if not os.path.isfile(pid_file):  # enforce pid lock file
          # attempt first fork
          if not _fork():
              return
  
          try:
              # detach from current environment
              os.chdir(working_dir)
              os.setsid()
              os.umask(0)
  
          except OSError:
              return
  
          # attempt second fork
          if not _fork():
              return
  
          # we'll ignore closing file descriptors..
          # redirecting the streams should be sufficient
  
          # redirect streams to /dev/null
          _fd = os.open(os.devnull, os.O_RDWR)
          os.dup2(_fd, sys.stdin.fileno())
          os.dup2(_fd, sys.stdout.fileno())
          os.dup2(_fd, sys.stderr.fileno())
  
          # setup pidfile
          _register_pidfile(pid_file)
  
          # execute provided callable
          func()
docs: Turns the current process into a daemon.
  
          `pid_file`
              File path to use as pid lock file for daemon.
          `working_dir`
              Working directory to switch to when daemon starts.
          `func`
              Callable to run after daemon is forked.