Processes and threads in Python

PS: We know that modern operating systems such as Mac OS X, UNIX, Linux, Windows, etc. are all operating systems that support "multitasking". There are three ways to realize multi-tasking: multi-process mode; multi-thread mode; multi-process + multi-thread mode. Python supports both multiprocessing and multithreading, and below we will discuss how to write multitasking programs for both.

refer to the original text

  Liao Xuefeng Python Processes and Threads

multi-Progress

In order to implement multiprocessing in a   Python program , let's first understand the relevant knowledge of the operating system.

  Unix/Linx operating system provides a fork( ) system call, which is very special, different from ordinary functions (call once, return once), fork() calls once and returns twice . This is because the operating system automatically copies the current process (parent process) (child process), and then returns in the parent process and the child process respectively.

  Returns 0 forever in the child process, and returns the child's ID in the parent process . This is done because a parent process can fork many child processes, so the parent process needs to write down the ID of each child process, and the child process only needs to call getppid() to get the ID of the parent process.

Common system calls are encapsulated in the os module   in Python, including fork , which can easily create subprocesses in Python programs:

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324983048&siteId=291194637