webserver.py 文件源码

python
阅读 28 收藏 0 点赞 0 评论 0

项目:invenio1-orcid 作者: bronger 项目源码 文件源码
def spawn_daemon(path_to_executable, env, *args):
    """Spawns a completely detached subprocess (i.e., a daemon).  Taken from
    http://stackoverflow.com/questions/972362/spawning-process-from-python
    which in turn was inspired by
    <http://code.activestate.com/recipes/278731/>.

    :Parameters:
      - `path_to_executable`: absolute path to the executable to be run
        detatched
      - `env`: environment
      - `args`: all arguments to be passed to the subprocess

    :type path_to_executable: str
    :type env: dict mapping str to str
    :type args: list of str
    """
    try:
        pid = os.fork()
    except OSError, e:
        raise RuntimeError("1st fork failed: %s [%d]" % (e.strerror, e.errno))
    if pid != 0:
        os.waitpid(pid, 0)
        return
    os.setsid()
    try:
        pid = os.fork()
    except OSError, e:
        raise RuntimeError("2nd fork failed: %s [%d]" % (e.strerror, e.errno))
    if pid != 0:
        os._exit(0)
    try:
        maxfd = os.sysconf(b"SC_OPEN_MAX")
    except (AttributeError, ValueError):
        maxfd = 1024
    for fd in range(maxfd):
        try:
           os.close(fd)
        except OSError:
           pass
    os.open(os.devnull, os.O_RDWR)
    os.dup2(0, 1)
    os.dup2(0, 2)
    try:
        os.execve(path_to_executable, [path_to_executable] + list(filter(lambda arg: arg is not None, args)), env)
    except:
        os._exit(255)
评论列表
文章目录


问题


面经


文章

微信
公众号

扫码关注公众号