Concurrent programming - multi-process

Concurrent programming - multi-process

Complicated by

Concurrency refers to perform multiple tasks simultaneously, concurrent programming means that applications written to support multiple tasks concurrently.

process

Process refers to a program is running, it is collectively referred to a series of processes, but also the operating system scheduling and resource allocation basic unit

The process is one way to achieve concurrent

A program can generate multiple processes

PID and PPID

PID : the system to process numbers

PPID : When a process opens a another process b, then b is called a parent process. When pycharm run py file, pycharm is the parent py file

Parallel and concurrent, blocking and non-blocking

Concurrency means that multiple events occur simultaneously, the need to have the handover complete

Parallel means that multiple events simultaneously, independently of each other

Blocking state is encountered because the IO operation, or SLEEP, leading to the subsequent code can not be executed by the CPU

Non-blocking contrast, it indicates that the program is being executed CPU.

Process has three states, ready state, running state, blocking state

python achieve multi-process

Python in two ways to open a child process

Embodiment 1: Example of the Process class

from multiprocessing import Process
import time

def task(name):
    print(f'{name} is running ')
    time.sleep(3)
    print('f{name} is fone')
if __name__=='__main__':
    #在windows系统之上,开启子进程的操作一定要放在这个里面
    #Process(target=task,kwargs={'name':egon})
    p=Process(target=task,args=('jack',))#参数放在元祖里
    p.start()   # 向操作系统发送请求,操作系统会申请内存空间,然后把父进程的数据拷贝给子进程,作为子进程的初始状态
    print('主')
    

Option 2: Inheritance Process, and cover the run method

from multiprocessing import Process
import time

class MyProcess(Process):
    def __init__(self,name):
        super(MyProcess,self).__init__()
        self.name=name

    def run(self):
        print('%s is running' %self.name)
        time.sleep(3)
        print('%s is done' %self.name)
if __name__ == '__main__':
    p=MyProcess('jack')
    p.start()
    print('主')

have to be aware of is

1. Open the child must be placed in the windows __main__below, because the windows open in the child process will reload all the code to create a process caused by recursive

2. The second way the code must be executed into the run method, the child process will perform the other not run regardless of the method

3.start just send a message to the operating system, the operating system to create a process that takes time, so there will be two cases sent

3.1 open process slower than the speed of program execution, first print the message "master" in the task of printing

3.2 open process faster than the speed of program execution, the first print task in the news, in print "master"

Inter-process memory isolated

Modify the global variables in a process, other processes are not affected. Like running two files, one modified, the other is not affected.

from multiprocessing import Process
import time
x=1000
def task():
    global x
    x=0
    print('儿子死啦',x)


if __name__ == '__main_
    print(x)
    p=Process(target=task)
    p.start()
    time.sleep(5)
    print(x)
    
###
1000
儿子死啦 0
1000

join function:

It allows a process to wait after the end of another process to run

from multiprocessing import Process
import time

x=1000

def task():
    time.sleep(3)
    global x
    x=0
    print('儿子死啦',x)
if __name__ == '__main__':
    p=Process(target=task)
    p.start()

    p.join() # 让父亲在原地等,如果不加join函数,则会先执行print(x)
    print(x)
    
from multiprocessing import Process
import time,random

x=1000

def task(n):
    print('%s is runing' %n)
    time.sleep(n)

if __name__ == '__main__':
    start_time=time.time()

    p1=Process(target=task,args=(1,))
    p2=Process(target=task,args=(2,))
    p3=Process(target=task,args=(3,))
    p1.start()
    p2.start()
    p3.start()

    p3.join() #3s
    p1.join()
    p2.join()

    print('主',(time.time() - start_time))

    start_time=time.time()
    p_l=[]
    for i in range(1,4):
        p=Process(target=task,args=(i,))
        p_l.append(p)
        p.start()
    for p in p_l:
        p.join()
    
    print('主',(time.time() - start_time))

Process object common attributes

from multiprocessing import Process
def task(n):
    print('%s is runing' %n)
    time.sleep(n)

if __name__ == '__main__':
    start_time=time.time()
    p1=Process(target=task,args=(1,),name='任务1')
    p1.start() # 启动进程
    print(p1.pid) # 获取进程pid
    print(p1.name) # 获取进程名字
    p1.terminate() # 终止进程
    p1.join() # 提高优先级
    print(p1.is_alive()) # 获取进程的存活状态
    print('主')

Orphaned and zombie process

Orphan process:

Refers to the child process after opening, the parent process before the child dies, then it is called the child process orphaned.

Orphan process is harmless, there is need for its existence, at the end of the parent process, child process which will be the operating system takes over.

Zombie process:

Refers to the first end when the parent process, and the parent has not recovered the child, the child process to release resources occupied, then the child will become a zombie process, this happens only in linux. Because the windows in the process is completely independent of no association.

If the parent process exits first, the child is to take over the operating system, the operating system will recover after the child process exits its resources occupied,

python has helped us to automatically handle the recycling zombie.

Guess you like

Origin www.cnblogs.com/zhoajiahao/p/11123086.html