multiprocess module

Parameter Description:

(1) group: parameter is not used, the value has not None

(2) targe: represents the calling object, task child process that is to be executed

(3) args: calling object indicates a position parameter tuples

(4) kwargs: indicates that the call object dictionary

(5) name: The name for the child process

Methods Introduction:

(1) p.start (): to start the process, and call the child process p.run ()

(2) p.run (): method of operation, when the process starts, it is the target to call the specified function, our custom class must implement this method in

(3) p.terminate (): p forcibly 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, resulting in deadlock

(4) p.is_alive (): If p is still running and returns True

(5) p.join ([timeout]): main thread wait terminated p (emphasized: in a state of the main thread and the like, and p is in an active state), the timeout timeout is optional, it is emphasized that, p.join can only join the main start open process, and can not join the live run open process

Property description:

(1) 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, you can not create p his new process must be p.start ()

Name of the process: (2) p.name

pid process: (3) p.pid

(4) p.exitcode: process at runtime to None, if -N, representing the end of the signal N (understand)

(5) p.authkey: Process authentication key, a default os.urandom () generates a random string of 32 characters, the use of the key is directed to provide communication between the underlying security of the network connection process, only the specific type of connection the same identity to be successful (understand) the verification key

Start of the first sub-process in python

import time
from multiprocessing import Process
def f(name):
    print('hello',name)
    print('我是子进程')
if __name__=='__main__':
    p=Process(target=f,args=('lzs',))
    p.start()
    time.sleep(1)
    print('我是主进程')

join method

import time
from multiprocessing import Process
def f(name):
    print('hello',name)
    print('我是子进程')
if __name__=='__main__':
    p=Process(target=f,args=('lzs',))
    p.start()
    p.join()
    print('我是主进程')

View of the main process and sub-process of process ID

import os
from multiprocessing import Process

def f(x):
    print('f子进程id:',os.getpid(),'父进程id:',os.getppid())
    return x*x
if __name__=='__main__':
    print('主进程id:',os.getpid())
    p_list=[]
    for i in range(5):
        p=Process(target=f,args=(i,))
        p.start()
       
##进阶,多个进程同时运行(注意,子进程的执行顺序不是根据启动顺序决定的)

Multiple processes running simultaneously

import time
from multiprocessing import Process

def f(name):
    print('hello',name)
    time.sleep(1)
if __name__=='__main__':
    p_slt=[]
    for i in range(5):
        p=Process(target=f,args=('lzs',))
        p.start()
        p_lst.append(p)       

Multiple processes running simultaneously, join join ()

import time
from multiprocessing import Process

def f(name):
    print('hello',name)
    time.sleep(1)
if __name__=='__main__':
    p_lst=[]
    for i in range(5):
        p=Process(target=f,args=('lzs',))
        p.start()
        p_lst.append(p)
        p.join()
    print('父进程在执行')

Daemon

With the end of the primary process will be ended

Create a master daemon process:

First: the daemon will terminate after the end of the main process code execution

Second: within daemon can no longer turn a child process, otherwise an exception is thrown

Note: the process is independent of each other, the main process code to finish, daemons immediately terminated

Guess you like

Origin www.cnblogs.com/lzss/p/11527499.html