day 28 Concurrent programming

一、multiprocessing

The multiprocessing module is used to start a child process and execute our customized tasks in the child process.

The multiprocessing module has many functions: supports subprocesses, communicates and shares data, performs different forms of synchronization, and provides components such as Process, Queue, Pipe, and Lock.

1、Process

You need to use keywords to specify parameters
args specifies the positional parameter passed to the target function, which is a tuple in the form of a comma, and kwargs passes a dictionary 
to create a subprocess 1
class MyProcess(Process):
  def __init__(self,name):
    super().__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('lg')
  p.start()
  print('main')
create subprocess 2
from multiprocessing import Process
import time
def take(name):
  print('%name is running'%name)
  time .sleep(3)
  print('%name is done'%name)

if __name__=='__main__': #On
  the windows system, the operation of starting the child process must be placed here
  p=Process(target==take,arge=('lg',))
  p.start()#Send a request to the operating system, the operating system will apply for memory space, and then copy the data of the parent process to the child process as a child the initial state of the process
  print('======main')

p.start(): start the process and call p.run() in the child process
p.run(): The method that runs when the process starts. It is the function that calls the target specified. This method must be implemented in the class of our custom class. 
p.name: The name of the process
p.pid: the pid of the process 
p.ppid: the pid of the parent process The pid
of the parent process is the pid of pycharm
 

The direct memory space of the process is isolated and physically isolated
from multiprocessing import Process
import time

x=1000

def task():
  time.sleep(1)
  global x
  x=0
  print('aaa',x)
if __name__==' __main__':
  print(x)
  p=Process(target=task)
  p.start()
  time.sleep(3)
  print(x)
p.terminate(): Forcibly terminate the process p without any cleanup operation. If p creates a child process, the child process will become a zombie process. Use this method with special care. If p also holds a lock, it will not be released, resulting in a deadlock
p.is_alive():如果p仍然运行,返回True
p.join([timeout]):主线程等待p终止(强调:是主线程处于等的状态,而p是处于运行的状态)。timeout是可选的超时时间,需要强调的是,p.join只能join住start开启的进程,而不能join住run开启的进程 
x=1000
def task(n):
  print('%s is runing'%n)
  time.sleep(n)
if __name__=='__main__':
  start_time=time.time()
  p1=Process(traget=task,args=(1,),name='任务1')
  p1.start()

  print(p1.pid)
  print(p1,name)
  p1.terminate()
  p1.join()
  print(p1.is_alive())
  print('=========主')


  僵尸与孤儿进程
  孤儿就是主进程干掉了,它产生的子进程没被干掉,系统就会定期回收,无害
  僵尸进程就是子进程干掉了但还留着pid只能等主进程被干掉后才会回收,如果主进程一直不死,这样会过多占用pid。
  linux用
from multiprocessing import Process
import time,os
def task(n):
  print('%s is running'%n)
  time.sleep(n)

if __name__=='__main__':
  p1=Process(tartget=task,args=(1,))
  p2=Process(tartget=task,args=(2,))
  p3=Process(tartget=task,args=(3,))  

  p1.start()
  p2.start()
  p3.start()
  p1.join()
  p2.join()
  p3.join()
  print('====主====',os.getpid())
  time.sleep(10000)
 









Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324807266&siteId=291194637