Home >Backend Development >Python Tutorial >Implementation of Python daemon

Implementation of Python daemon

巴扎黑
巴扎黑Original
2017-08-16 13:39:281507browse

Daemon Scenario

Consider the following scenario: you write a python service program and start it from the command line, and your command line session is controlled by the terminal, and the python service becomes a part of the terminal program child process. So if you close the terminal, the command line program will also close.

To make your python service permanent in the system without being affected by the terminal, you need to turn it into a daemon process.

The daemon process is a Daemon program, which is a program executed in the background of the system. It is independent of the control terminal and performs some periodic tasks or triggers events. It is usually named with the letter "d" at the end, such as the common httpd , syslogd, systemd and dockerd, etc.

Code implementation

Python can implement the daemon process very simply. The code and corresponding comments are given below. This code runs stably in a daemon process (self-made alarm clock) on my local computer, and there have been no problems for the time being.

# coding=utf8
import os
import sys
import atexit
def daemonize(pid_file=None):
    """
    创建守护进程
    :param pid_file: 保存进程id的文件
    :return:
    """
    # 从父进程fork一个子进程出来
    pid = os.fork()
    # 子进程的pid一定为0,父进程大于0
    if pid:
        # 退出父进程,sys.exit()方法比os._exit()方法会多执行一些刷新缓冲工作
        sys.exit(0)
    # 子进程默认继承父进程的工作目录,最好是变更到根目录,否则回影响文件系统的卸载
    os.chdir('/')
    # 子进程默认继承父进程的umask(文件权限掩码),重设为0(完全控制),以免影响程序读写文件
    os.umask(0)
    # 让子进程成为新的会话组长和进程组长
    os.setsid()
    # 注意了,这里是第2次fork,也就是子进程的子进程,我们把它叫为孙子进程
    _pid = os.fork()
    if _pid:
        # 退出子进程
        sys.exit(0)
    # 此时,孙子进程已经是守护进程了,接下来重定向标准输入、输出、错误的描述符(是重定向而不是关闭, 这样可以避免程序在 print 的时候出错)
    # 刷新缓冲区先,小心使得万年船
    sys.stdout.flush()
    sys.stderr.flush()
    # dup2函数原子化地关闭和复制文件描述符,重定向到/dev/nul,即丢弃所有输入输出
    with open('/dev/null') as read_null, open('/dev/null', 'w') as write_null:
        os.dup2(read_null.fileno(), sys.stdin.fileno())
        os.dup2(write_null.fileno(), sys.stdout.fileno())
        os.dup2(write_null.fileno(), sys.stderr.fileno())
    # 写入pid文件
    if pid_file:
        with open(pid_file, 'w+') as f:
            f.write(str(os.getpid()))
        # 注册退出函数,进程异常退出时移除pid文件
        atexit.register(os.remove, pid_file)

Summarize the steps for writing a daemon process:

1. Fork the child process and exit the parent process

2. The child process changes the working directory (chdir) and file permissions Mask (umask), process group and session group (setsid)

3. The child process forks the grandson process and exits the child process

4. The grandson process refreshes the buffer and redirects standard input/output / Error (usually to /dev/null, meaning discarded)

5. (Optional) pid is written to the file

Understand a few key points

Why do we need to fork two Times

The first fork is to escape from the clutches of terminal control. The reason why the parent process exits is because the terminal hits the keyboard or sends a signal to it when it is closed; and the forked child process becomes an orphan process after the parent process commits suicide, and is then taken over by the init process of the operating system, so it leaves the terminal. control.

So in fact, the second fork is not necessary (the code in many open source projects does not fork twice). It's just out of caution to prevent the process from opening a control terminal again. Because the child process is now the session leader (the first process in the session) and has the ability to open the control terminal, if it forks again, the grandson process will not be able to open the control terminal.

File descriptor

Linux is "everything is a file". The file descriptor is the index created by the kernel for the opened file, usually a non-negative integer. Processes perform IO operations through file descriptors.

Each process has its own file descriptor table, so the same descriptor may point to the same file or different files; different descriptors from different processes may of course point to the same file. .

By default, 0 represents standard input, 1 represents standard output, and 2 represents standard error.

umask permission mask

We know that in Linux, any file has three permissions: read (read), write (write) and execute (execute). Among them, the read permission is represented by the number 4, the write permission is 2, and the execution permission is 1. You can check the file permissions with the command ls -l, and r/w/x means you have read/write/execute permissions respectively.

Any file also has three identity permissions: User, Group, and Others. Generally, three numbers are used to represent file permissions, such as 754:

7, which is User permission, that is, the file owner permission

5, which is Group permission, and belongs to the members of the user group where the owner belongs. The permissions

4 it has are Others permissions, that is, the permissions of users in other groups

And umask is to control the default permissions and prevent new files or folders from having full permissions.

The system generally defaults to 022 (use the command umask to view), which means that the default permissions for creating files are 644 and folders are 755. You should be able to see their pattern, that is, the sum of file permissions and umask is 666 (laughing), and the sum of folder permissions and umask is 777.

Process Group

Each process belongs to a process group (PG, Process Group), and a process group can contain multiple processes.

The process group has a process leader (Leader), and the ID of the process leader (PID, Process ID) is used as the ID of the entire process group (PGID, Process Groupd ID).

Session Group

When you log in to the terminal, a session will be created. Multiple process groups can be included in one session. The process that creates a session is the session leader.

A process that is already the session leader cannot call the setsid() method to create a session. Therefore, in the above code, the child process can call setsid(), but the parent process cannot because it is the session leader.

In addition, sh (Bourne Shell) does not support the session mechanism, because the session mechanism requires the shell to support job control (Job Control).

Daemon process and background process

Using the & symbol, commands can be executed in the background. It is different from the daemon process:

1. The daemon process has nothing to do with the terminal and is an orphan process adopted by the init process; while the parent process of the background process is the terminal, it can still print on the terminal

2. The daemon process remains strong when closing the terminal; while the background process will stop when the user exits, unless nohup

3 is added. The daemon process changes the session, process group, working directory and file descriptor. The background process directly inherits the

of the parent process (shell)

In other words: the daemon process is the promising young man who works hard silently, while the background process is the rich second generation who silently inherits his father's assets.

The above is the detailed content of Implementation of Python daemon. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn