112 Python program in the process of operation - open multi-process (multiprocess.process)

Running program is a process. All the processes are by its parent to create . Therefore, running the python program is a process, then we can re-create the process in the program. Multiple processes concurrently effect can be achieved when there are multiple processes of our program, at some point, it will allow faster execution speed of the program.

Fork function to create threads in linux c language, and Python will need the help of a response module

A, multiprocess module

Careful to say, multiprocess module but not a python in one operation, package management processes. It is called multi taken from multiple multi-functional meaning in this package includes almost all sub-modules and related processes. Because so many sub-module, in order to facilitate classify remember, this part of me roughly divided into four parts: part creation process, process synchronization part, the data pool portion between processes, process sharing.

Two, multiprocess.process module

process module is a module process of creating, by means of this module, you can complete the creation process.

Three, process module

Process([group [, target [, name [, args [, kwargs]]]]]), Obtained by the object class is instantiated, showing a sub-process tasks (not yet started)

Emphasize:

  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,'egon',)
  • kwargs indicates that the call object dictionary,kwargs={'name':'egon','age':18}
  • name is the name of the child process

3.1 Method 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

3.2 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

3.3 Note the use of the Process class in the 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.

Fourth, the process using the class

Open a child process, start method and concurrent effect of a python process.

4.1 Create and open sub-process in two ways

  • One way: by function

    from multiprocessing import Process
    import time
    
    '''
    开启子进程的两种方式:
    1. 通过函数
    2. 通过类。继承Process
    '''
    
    def task(name):
        print("进程{%s} start"%(name))
        time.sleep(2)
        print(f"进程{name} end")
    
    # 必须加main
    if __name__ == '__main__':
        ######## 方式1(通过函数)
        p = Process(target=task,args=("xc",))   # args用于传参,是个元祖,必须加逗号
        p.start() # 告诉操作系统启动子进程,但一定是父进程先执行,多个子进程的执行顺序是根据操作系统调度决定的
        print('主进程/父进程')
        print('主进程/父进程')
        p1 = Process(target=task,args=("cyx",))
        p1.start()  # 告诉操作系统启动子进程,但一定是父进程先执行,多个子进程的执行顺序是根据操作系统调度决定的
        print('主进程/父进程')
        print('主进程/父进程')
        print('主进程/父进程')
        print('主进程/父进程')
  • Second way: by class. Inheritance Process

    from multiprocessing import Process
    import time
    
    '''
    开启子进程的两种方式:
    1. 通过函数
    2. 通过类。继承Process
    '''
    
    class myProcess(Process):
        def __init__(self,name):
            # self.name = name #错误 ### 这样没有给对象添加属性name,而是在修改父类的进程名(name)
    
            # 父类Process的进程名也是name
            super().__init__()  # 调用父类super().init方法完成创建进程初始化,重新给name属性赋值了。
            self.name = name    ## 在父类的init方法后设置name,才是为自己对象添加属性
    
            # super().__init__(name=name)  # 调用父类super().init,并设置进程名(name)
    
    
        def run(self):  # 创建进程会默认调用run方法
            print("进程%s start" % (self.name))
            time.sleep(2)
            print(f"进程{self.name} end")
    
    # 必须加main
    if __name__ == '__main__':
        p = myProcess("xc")
        p.start()
        print('主进程/父进程')
        print('主进程/父进程')
        p1 = myProcess("cyx")
        p1.start()  # 告诉操作系统启动子进程,但一定是父进程先执行,多个子进程的执行顺序是根据操作系统调度决定的
        print('主进程/父进程')
        print('主进程/父进程')
        print('主进程/父进程')
        print('主进程/父进程')

4.2 join method

join method for the recovery of the child

from multiprocessing import Process
import time
  
def foo(x):
      print('进程  start ')
      time.sleep(x)
      print('进程  end ')
      
if __name__ == '__main__':
      ### 串行执行和回收子进程
      # p = Process(target=foo, args=(1,))
      # p2 = Process(target=foo, args=(2,))
      # p3 = Process(target=foo, args=(3,))
      # p.start() #
      # p.join() # 阻塞住主进程再等待子进程结束,然后再往下执行,(了解的是:内部会待用wait())
      # p2.start()
      # p2.join()
      # p3.start()
      # p3.join()
      # print('主')
  
      # 并发执行进程,并依次回收
      p = Process(target=foo, args=(1,))
      p2 = Process(target=foo, args=(2,))
      p3 = Process(target=foo, args=(3,))
      # 开启进程
      p.start()
      p2.start()
      p3.start()
  
      # 回收进程
      p.join() # 阻塞住主进程再等待子进程结束,然后再往下执行,(了解的是:内部会待用wait())
      p2.join()
      p3.join()
      print('主')

4.3 Check the main and the child's process ID

from multiprocessing import Process,current_process
import time
import os
  
  '''
  查看主进程和子进程的进程号
  1. 通过os.getpid()方法
  2. 通过multiprocessing模块中的current_process().pid
  '''
  
  def task(name,x):
      print("当前进程pid:", current_process().pid)
      print(f"{name} start")
      time.sleep(x)
      print(f"{name} end")
  
  if __name__ == '__main__':
      p = Process(target=task,args=("进程1",1))
      p.start()
      # 方式一
      print("子进程pid:",p.pid)
      # 方式二
      # print("当前进程pid:",current_process().pid)
      print("当前进程pid",os.getpid())
      print("主进程的父进程pid",os.getppid())    # 实际上是pycharm的进程号
      print()

4.4 view the process name and process status, set the process name

'''
process设置名字: name属性
process判断进程是否存在:is_alive
'''

from multiprocessing import Process
import time
  
  def task(x):
      print("进程 start")
      time.sleep(x)
      print("进程 end")
  
  if __name__ == '__main__':
      p = Process(target=task,args=(1,))
      p.start()
      p.name = "进程1"
      print(p.name)
  
      print("子进程是否存在:", p.is_alive()) # True
      time.sleep(2)   # 延时2秒等待子进程结束
      print("子进程是否存在:", p.is_alive()) # False
  
      print("主进程")

4.5 terminate the end of the child process

'''
terminate()
告诉子进程让他结束
'''

from multiprocessing import Process
import time
def task(x):
    print("进程 start")
    time.sleep(x)
    print("进程 end")

if __name__ == '__main__':
    p = Process(target=task,args=(10,))
    p.start()
    p.terminate()   # 告诉子进程让他提前结束

    p.name = "进程1"
    print(p.name)

    print("子进程是否存在:", p.is_alive()) # True
    p.join()
    print("子进程是否存在:", p.is_alive()) # False

    print("主进程")

Daemon 4.6 Process of

First of all, bloggers own test, test. Process and real part of the concept of daemons daemon is not the same, so only need to know the Process daemon can be.

'''
daemon = True 把子进程变为守护进程
主进程的代码执行完毕守护进程直接结束。但如果子进程代码结束也会结束
'''
from multiprocessing import Process
import time
def task(x):
    print("进程 start")
    time.sleep(x)
    print("进程 end")


if __name__ == '__main__':
    p = Process(target=task,args=(2,))
    p.daemon = True  # 把子进程变为守护进程
    p.start()
    # print(p.pid)

    p.name = "进程1"
    print(p.name)
    print("子进程pid:", p.pid) #

    print("子进程是否存在:", p.is_alive()) # True
    time.sleep(3)
    print("子进程是否存在:", p.is_alive()) # False

    print("主进程")
    print("子进程是否存在:", p.is_alive())  # False

    time.sleep(200)

Five, socket chat concurrent instances

5.1 Use multiple processes to achieve concurrent chat socket -server end

import socket
from multiprocessing import Process

def talk(conn,client_addr):
    while 1:
            msg = conn.recv(1024)
            if not msg:
                break
            print(msg.decode("utf8"))
            conn.send(msg.upper())
            print(111)

if __name__ == '__main__':
    # 必须要写在里面,不然会因为创建子线程重复调用导致端口被占用
    server = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    server.bind(("127.0.0.1", 8087))
    server.listen(5)

    while 1:
        print("等待连接")
        conn,addr = server.accept()
        print(addr,"连接成功")
        p = Process(target=talk,args=(conn,addr))
        p.start()

5.2 Use socket chat multiple processes to achieve concurrent -client end

import socket

if __name__ == '__main__':
    client = socket.socket()
    client.connect(("127.0.0.1",8087))
    while 1:
        msg = input("请输入内容")
        client.send(msg.encode("utf8"))
        msg = client.recv(1024).decode("utf8")
        print(msg)

Guess you like

Origin www.cnblogs.com/XuChengNotes/p/11515329.html