Create a Multi-process of multiprocess package process module


Create a Multi-process of multiprocess package process module


1.process module is a module process of creation

Process([group [, target [, name [, args [, kwargs]]]]]) The object of the class get instantiated, showing a sub-process task

He stressed :

  1. You need to use the keyword way to specify parameters
  2. args specified target position for the transfer function parameters, is in the form of a tuple, there must be a comma

Parameter Description :

  • group parameter is not used, the values ​​are always None
  • target indicates that the call target, that is the task to be executed child process
  • args parameter indicates the position of the tuple call the object,args=(1,2,'cxk',)
  • kwargs indicates that the call object dictionary,kwargs={'name':'cxk','age':98}
  • name is the name of the child process

Methods Introduction

  • p.start(): Start the process and call the child process p.run ()
  • p.run(): Method of operation, when the process starts, it is the function to call the specified target, we custom classes in this method must be achieved
  • p.terminate(): P forced to terminate the process, will not carry out any cleaning operation, if p create a child process, the child process to become a zombie process, this method requires special care this situation. If p then also saved a lock will not be released, leading to a deadlock
  • p.is_alive(): If p is still running and returns True
  • p.join([timeout]): P wait for the main thread to terminate (stressed: the state is the main thread in the other, and p is in a state of operation). timeout is optional timeout should be emphasized that, p.join can join live start open process, and can not join the live run open process

Introduction property

  • p.daemon: The default value is False, if set to True, the representative of p is a daemon running in the background, when the termination of the parent process p, p also will be terminated, and after setting True, p can not create their own new process must in the p.start()before setting
  • p.nameName of the process:
  • p.pid: Pid process
  • p.exitcode: The process at runtime None, if -N, representing the end of the signal N (to understand)
  • p.authkey: Identity verification process key, default by os.urandom()a 32-character randomly generated string. To succeed (to understand) when the key purpose is to provide security for the network connection between the underlying processes involved in communication, this type of connection only with the same authentication key

Note the use of process modules in Windows

In the Windows operating system, because there is no fork (the process of creating a mechanism for the linux operating system), when creating the child process will automatically import this file to start it, but when they perform in the import of the entire file. So if the process () directly written in the file will create a child process infinite recursion error. It is necessary to create a child process of partially used if __name__ =='__main__'to determine protected, import time, it will not run recursively.

import time
from multiprocessing import Process

def func(name):
    print('hello',name)
    time.sleep(2)
    print('我是子进程')

if __name__ == '__main__':
    p = Process(target=func,args=('usb',))
    p.start()    # 启动进程,并调用该子进程中的p.run()后开始运行,告诉操作系统我要开子进程,告诉完了这行代码就算执行完了,接着往下走,
                    # 具体操作系统什么时候开子,开多长时间跟你没关系。
    # time.sleep(3)
    print('执行主进程的内容')

Cantabile open a child process?

method one:

from multiprocessing import Process
import time

def task(x):
    print(f'子进程{x} start')
    time.sleep(2)
    print(f'子进程{x} end')


if __name__ == '__main__':
    p = Process(target=task,args=('cxk',))
    p2 = Process(target=task,args=('nike',))
    p.start()  # 告诉操作系统我要开子进程,告诉完了这行代码就算执行完了,接着往下走,具体操作系统什么时候开子,开多长时间跟你没关系。
    p2.start()
    time.sleep(4)
    print('我是父进程')

Second way :( not recommended)

from  multiprocessing import Process
import time

class Tset(Process):
    def __init__(self,gender):
        super().__init__()
        self.gender = gender

    def run(self):
        print(f'子进程是{self.gender} start')
        time.sleep(2)
        print('子进程 end')

if __name__ == '__main__':
    p = Tset('基佬')
    p.start()  # 向操作系统 发送开启子进程的请求
    # time.sleep(3)
    print('我是主进程')

Memory data isolation verification process

from multiprocessing import Process
import time

x=0
def task():
    global x
    x= 100
    print('子进程的x修改为了{}'.format(x))

if __name__ == '__main__':
    p = Process(target=task)
    p.start()
    time.sleep(5)
    print(x)

The join process modules, use:

p.join([timeout]): P wait for the main thread to terminate (stressed: the state is the main thread in the other, and p is in a state of operation). timeout is optional timeout should be emphasized that, p.join can join live start open process, and can not join the live run open process

method one:

from multiprocessing import Process
import time

def foo():
    print('进程 start')
    time.sleep(3)
    print('进程 end')

if __name__ == '__main__':
    p = Process(target=foo)
    p.start()
    p.join()
    print('我是主进程')

Method Two:

from  multiprocessing import Process
import time

def foo(x):
    print('进程 start')
    time.sleep(x)
    print('进程 end')

if __name__ == '__main__':
    p1 = Process(target=foo,args=(1,))
    p2 = Process(target=foo,args=(2,))
    p3 = Process(target=foo,args=(3,))
    start = time.time()
    p1.start()
    p2.start()
    p3.start()

    p1.join()
    p2.join()
    p3.join()
    end = time.time()  # 计算三个子进程的运行时间
    print(end-start)   # 3.83371901512146
    print('我是主进程')

Mode 3:

from multiprocessing import Process
import time

def foo(x):
    print(f'进程{x} start')
    time.sleep(x)
    print(f'进程{x} end')

if __name__ == '__main__':
    p1 = Process(target=foo,args=(1,))
    p2 = Process(target=foo,args=(2,))
    p3 = Process(target=foo,args=(3,))
    start = time.time()
    p1.start()
    p1.join()

    p2.start()
    p2.join()

    p3.start()
    p3.join()

    # 不如不开,直接穿行调用函数反而快
    # foo(1)
    # foo(2)
    # foo(3)

    end = time.time()
    print(end-start) # 7.275144100189209
    print('我是主进程')

Four: (Code Optimization Method II)

from  multiprocessing import Process
import time

def foo(x):
    print(f'进程{x} start')
    time.sleep(x)
    print(f'进程{x} end')

if __name__ == '__main__':
    start = time.time()
    p_list = []
    for i in range(1,4):
        p = Process(target=foo,args=(i,))
        p.start()
        p_list.append(p)
    print(p_list)
    for p in p_list:
        p.join()
    end = time.time()   
    print(end-start)  # 3.721641778945923
    print('我是主进程')

pid and ppid of use:

from  multiprocessing import Process,current_process
import time,os


def func():

    print('子进程 start')
    print('在子进程中查看自己的Pid',current_process().pid)
    print('在子进程中查看父进程的pid',os.getpid())
    time.sleep(200)
    print('子进程 end')

if __name__ == '__main__':
    p = Process(target=func)
    p.start()
    print('在主进程查看子进程的pid',p.pid) # 一定要写在 start() 之后
    print('主进程的pid:',os.getpid())
    print('主进程的父进程pid:',os.getppid())
    print('我是主进程')

** remember to ok this is to grasp the
stand in the perspective of the current process
os.getpid () # Get the current process
pidos.getppid () # Get the parent process pid of the current process
child process to obtain the object .pid # child process pid the current process


name and is_alive usage (understand)

from multiprocessing import Process,current_process
import time

def foo():
    print('进程 start')
    print('-----------',current_process().name)
    time.sleep(2)
    print('进程 end')

if __name__ == '__main__':
    p = Process(target=foo)
    p2 = Process(target=foo)
    p3 = Process(target=foo,name='cxk')  # 自定义进程

    p.start()
    p2.start()
    p3.start()
    print(p.is_alive())  # True
    time.sleep(5)
    print(p.is_alive())  # 代码运行完了就算该进程已经结束了(死了)False
    print(p.name)
    print(p2.name)
    print(p3.name)
    print('我是主函数')

terminate the use of:

p.terminate(): P forced to terminate the process, will not carry out any cleaning operation, if p create a child process, the child process to become a zombie process, this method requires special care this situation. If p then also saved a lock will not be released, leading to a deadlock

from  multiprocessing import Process
import time

def foo():
    print('进程 start')
    time.sleep(50)
    print('进程 end')

if __name__ == '__main__':
    p = Process(target=foo)

    p.start()
    p.terminate()  # 给操作系统发送了一个强行终止子进程的请求
    print(p.is_alive())   # True  系统还没处理到终止请求 ,所以为True
    p.join()
    print(p.is_alive())  # False
    print('我是主进程')

Daemon:

Guardian - "With

Nature is a child process

The code of the primary process is finished daemon is ended. But this time the main process may not be over. '' '

from multiprocessing import Process
import time

def foo():
    print('守护进程 start')
    time.sleep(5)
    print('守护进程 end')

if __name__ == '__main__':
    p = Process(target=foo)
    p.daemon = True  # 把这个子进程定义为守护进程
    p.start()
    time.sleep(2)
    print('我是主进程')

Daemon 2:

from multiprocessing import Process
import time

def foo():
    print('守护进程 start')
    time.sleep(3)
    print('守护进程 end')

def func():
    print('子进程 start')
    time.sleep(5)
    print('子进程 end')

if __name__ == '__main__':
    p = Process(target=foo)
    p2 = Process(target=func)
    p.daemon = True  # 把这个子进程定义为守护进程
    p.start()
    p2.start()
    time.sleep(1)
    print('我是主进程')

Exercise: grab votes applet:

from  multiprocessing import Process
import json,time,os

def search():
    time.sleep(1)  # 模拟网络io
    with open('db.txt',mode='rt',encoding='utf-8') as fr:
        res = json.load(fr)
        print(f'还剩{res["count"]}张票')


def get():
    with open('db.txt',mode='rt',encoding='utf-8') as fr:
        res = json.load(fr)
    time.sleep(1)  # 模拟网络io
    if res['count'] > 0:
        res['count'] -= 1
        with open('db.txt',mode='wt',encoding='utf-8') as fw:
            json.dump(res,fw)
            print(f'进程{os.getpid()} 抢票成功')
        time.sleep(1.5)  # 模拟网络io

    else:
        print('票已售罄!!!')

def task():
    search()
    get()

if __name__ == '__main__':
    for i in range(10):
        p = Process(target=task)
        p.start()
        p.join()

# 为了保证数据的安全,要牺牲掉效率

Guess you like

Origin www.cnblogs.com/guapitomjoy/p/11515567.html