Several ways to create a process in python

In the newly created child process, the parent of all the information will copy the data between them independently of each other.

Use os.fork () to create

The only way for Unix / Linux operating system, can not be used in windows.

import os

# 注意,fork函数,只在Unix/Linux/Mac上运行,windows不可以
pid = os.fork()
# 子进程永远返回0,而父进程返回子进程的ID。
if pid == 0:
	print('子进程')
else:
	print('父进程')

Classes created using Process

multiprocessing module provides a Process class to represent a process object, the following example demonstrates starting a child process and waits for its end:

from multiprocessing import Process
import time

def test(name, age):
    for i in range(5):
        print("--test--%s\t%d" % (name, age))
        time.sleep(1)
    print("子进程结束")


if __name__ == '__main__':
    p = Process(target=test, args=("aaa", 18))
    p.start()
    # 等待进程实例执⾏结束,或等待多少秒;
    p.join() # 等待的最长时间
    print("主进程结束")
"""
输出结果:
--test--aaa	18
--test--aaa	18
--test--aaa	18
--test--aaa	18
--test--aaa	18
子进程结束
主进程结束
"""

join () method represents the main process waits for the child process to continue down the implementation is completed, if the join () commented out, the main process has been started without a break down to continue after the child, and then wait for the child to complete the program ends.
The join () method commented results:

"""
输出结果:
主进程结束
--test--aaa	18
--test--aaa	18
--test--aaa	18
--test--aaa	18
--test--aaa	18
子进程结束
"""

Subclass created using Process

Create a new process can also be used like fashion, you can customize a class that inherits the Process class, each instance of this class, is equivalent to an example of a process object, see the following examples:

from multiprocessing import Process
import time
import os


class MyProcess(Process):

    def __init__(self):
        # 如果子类要重写__init__是必须要先调用父类的__init__否则会报错
        # Process.__init__(self)   
        super(MyProcess,self).__init__()

    # 重写Porcess的run()方法
    def run(self):
        print("子进程(%s)开始执行,父进程(%s)" % (os.getpid(), os.getppid()))
        for i in range(5):
            print("--1--")
            time.sleep(1)


if __name__ == '__main__':
    t_start = time.time()
    p = MyProcess()
    p.start()
    # p.join()
    print("main")
    for i in range(5):
        print("--main--")
        time.sleep(1)

Create a process using the pool Pool

When a small number of child processes need to be created, you can directly use multiprocessing in Process born into a dynamic multiple processes, but if it is hundreds or even thousands of targets, huge manual effort to create a process, then you can use Pool the method multiprocessing module.
When initialization Pool, you can specify a maximum number of processes, when a new request is submitted to the Pool, if the pool is not yet full, it will create a new process for executing the request; but if the number of processes that the pool has been maximum specified, then the request will wait until the end of the process there is the pool, will create a new process to execute, see example below:

from multiprocessing import Pool
import os
import time


def worker(num):
    # for i in range(3):
    print("----pid=%d  num=%d---" % (os.getpid(), num))
    time.sleep(1)

if __name__ == '__main__':
	# 定义一个进程池,最大进程数3
    pool = Pool(3)
    for i in range(10):
        print("---%d--" % i)
        # 使用非阻塞方式调用func(并行执行),一般用这个。
        # apply堵塞方式必须等待上一个进程退出才能执行下一个进程,用的不多。
        pool.apply_async(worker, (i,))
    # 关闭进程池
    pool.close()
    # 等待所有子进程结束,主进程一般用来等待
    pool.join()  # 进程池后面无操作时必须有这句
Published 44 original articles · won praise 8 · views 2466

Guess you like

Origin blog.csdn.net/qq_39659278/article/details/100025325