When to use multi-process programming in python

(*-*)浩
Release: 2019-06-25 11:02:06
Original
2177 people have browsed it

In concurrent programming, multi-threading and multi-process are two modes that are often used (in addition to coroutines, etc.). Due to CPython's GIL limitations (Jython and IronPython do not have GIL, PyPy is trying to remove GIL), only threads that have obtained GIL can use the CPU, so in addition to needing to handle some IO that may be blocked (reading and writing files, accessing the network, etc. ), basically no one will use CPython's multi-threading. Therefore, this article will talk about more useful Python multi-process programming.

When to use multi-process programming in python

Note: Recommended learning: Python video tutorial)

The environment described in this article is CPython under the Linux operating system (also applicable to most POSIX systems) may not be applicable to Windows operating systems or other Python implementations.

In order to avoid ambiguity, "main process" or "current process" is used below to refer to the process that created the child process, and "parent process" is not used, unless "xx's parent process" is explicitly specified.

Different from the need to pass a callable object during multi-thread programming, during multi-process programming, the main process is copied to the child process, and the child process cannot be directly required to execute a callable object.

In the POSIX system, this copy operation is completed by the clone() and fork() system calls, and the latter is generally used.
If fork() is executed successfully, the PID and 0 of the child process will be returned in the main process and the child process respectively, and then the execution code will start to be different. If it fails (not enough memory, PID reaches the upper limit, etc.), the child process will not be created, the main process will return -1, and errno will be set to the corresponding error code.
In the implementation of CPython, os.fork() is mainly an encapsulation of the fork() function. The difference is that an OSError is thrown when it fails. The errno attribute of the exception is the corresponding error code.

Therefore, the multi-process Python code will roughly look like this:

import os

try:
    pid = os.fork()
    if pid == 0:  # 子进程
        # 子进程的代码
    else:  # 主进程
        # 主进程的代码
except OSError:
    # 主进程处理 fork 失败的代码
Copy after login

For more Python related technical articles, please visit the Python Tutorial column Get studying!

The above is the detailed content of When to use multi-process programming in python. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!