Application process

What is the process

Process: a process or a task in progress, the data used by each process in memory is the physical level of isolation from each other

Example (monocytes + multi-channel, to achieve concurrent execution of multiple processes):

I was in a period of time there are many things to do, such as cooking, water heating, washing clothes, but I can only do one thing. But if every thing I did after a pause and do something else, that does not guarantee all my things are in progress Well

The difference between the process and procedures?

Difference: a process in progress or a task, and the program only only just a bunch of code.

Process of multiprocessing module

Features:

  • Many features multiprocessing module: support for the child process, communicate and share data and synchronize different forms, provides Process, Queue, Pipe, Lock and other components.

  • Need to re-emphasize is this: Unlike threads, processes, no shared state, the process of modifying the data, only changes in the process.

Process

****** ****** parameters introduced
group参数未使用,值始终为None

target表示调用对象,即子进程要执行的任务

args表示调用对象的位置参数元组,args=(1,2,'egon',)

kwargs表示调用对象的字典,kwargs={'name':'egon','age':18}

name为子进程的名称

Methods Introduction

p.start():启动进程,并调用该子进程中的p.run() 
p.run():进程启动时运行的方法,正是它去调用target指定的函数,我们自定义类的类中一定要实现该方法  
p.terminate():强制终止进程p,不会进行任何清理操作,如果p创建了子进程,该子进程就成了僵尸进程,使用该方法需要特别小心这种情况。如果p还保存了一个锁那么也将不会被释放,进而导致死锁
p.is_alive():如果p仍然运行,返回True

p.join([timeout]):主线程等待p终止(强调:是主线程处于等的状态,而p是处于运行的状态)。timeout是可选的超时时间,需要强调的是,p.join只能join住start开启的进程,而不能join住run开启的进程  

Create a process in two ways

The first way to create a process of:

###开启子进程方式一
#导入模块
from multiprocessing import Process

import time

#写进程
def zi():
    print('进程开启')
    time.sleep(3)
    print('进程结束')

#win 10下必须写
if __name__ == '__main__':
    #创建进程
    p = Process(target= zi)
    #启动进程
    p.start()

The second way to create a process:

###开启进程方式二
#导包

from multiprocessing import Process

import time


#通过自定义类的方式启动进程
#继承Process
class My(Process):
    def __init__(self):
        super().__init__()

    def run(self):
        print('进程启动-----')
        time.sleep(3)
        print('进程结束-----')


if __name__ == '__main__':
    p = My()
    p.start()

join method of process

Serial:

  • Inside the same time, programs are sequentially executed in sequence

parallel:

  • Inside the same time period, while the implementation of the program

join method:

from multiprocessing import Process
import time

def dask():
    print('进程开启——————————')
    time.sleep(5)
    print('进程结束——————————')

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

---------- process open
end of the process ----------
primary process

from multiprocessing import Process
import time

def dask():
    print('进程开启——————————')
    time.sleep(5)
    print('进程结束——————————')

if __name__ == '__main__':
    p = Process(target= dask)
    p.start()
    #p.join()
    print('主进程')

Main process
process open ----------
End Process ----------

Guess you like

Origin www.cnblogs.com/ledgua/p/11529506.html