Interprocess communication and process considerations

1.Interprocess communication-Queue Queue:
        1.Queue introduction( Queue): 
            //A common data structure that uses first in, first out to write data from the end of the queue and get data from the head of the queue

        2.Use of queues:
            -Queue(maxsize=3):Building queue objects, maxsize= 3 Indicates that the maximum value in the queue is 3
            -put(): Save messages to the queue, put News. If the queue is full, wait
            -get():Get messages from the queue, wait if the queue is empty
            -full():Determine whether the queue is full
            -empty();Judge whether the queue is empty
        3.Reference code--Communication between queue implementation processes
            import multiprocessing
            import time
            def write_data(q):
                for i in "python":
                    time.sleep(0.2)
                    q.put(i)
                    print("put %s to queue..."%i)
            def read_data(q):
                while not q.empty():
                    time.sleep(0.2)
                    v = q.get()
                    print("get %s from queue..."%v)

            if __name__ == '__main__':
                q = multiprocessing.Queue()
                p1 = multiprocessing.Process(target=write_data,args=(q,))
                p2 = multiprocessing.Process(target=read_data, args=(q,))
                p1.start()
                p1.join()


                p2.start()
                p2.join()
                print("--------") 

    4.The process communication between the process pools also uses queues for communication
            1.Reference code: 
    import multiprocessing
    import random

    import time

    def write_data():
        for i in "python":
            time.sleep(1)
            q.put(i)
            print("put %s to queue"%i)
    def read_data():
        while not q.empty():
            v = q.get()
            time.sleep(1)
            print("get %s from queue" % v)

    if __name__ == '__main__':
        #Multitasking can be done through the process pool, but this method is used when there are many tasks
        #When the number of tasks is greater than the maximum value of the process pool, a part of the processes will be in the waiting state once entering the
        # The code in the pool will switch to another subprocess after running

        #Be careful! If you use pool to create a process, you need to use multiprocessing.Manager().Queue() to create it, otherwise there will be problems
        q = multiprocessing.Manager().Queue()
        pool = multiprocessing.Pool()

        pool.apply_async(func=write_data())
        pool.apply_async(func=read_data())
        pool.close()
        pool.join()

2.How to view processes id
        1.View the current process's id  : os.getpid()  #get process id
        2.View parent process id  :  os.getppid  #get parent process id

        //View through ps -aux | grep '01 -' in linux
            //Reference code:
    import multiprocessing
    import os

    import time


    def task1():
        while True:
            time.sleep(1)
            print("task1  Current process id=%d,Parent process id=%d"%(os.getpid(),os.getppid()))


    def task2():
        while True:
            time.sleep(1)
            print("task2  Current process id=%d,Parent process id=%d" % (os.getpid(), os.getppid()))


    if __name__ == '__main__':
        p1 = multiprocessing.Process(target=task1)
        p2 = multiprocessing.Process(target=task2)
        p1.start()
        p2.start()

    3.Process Grammatical structure
        1.The introduction of function transfer parameter in subprocess
            1.adopt Process([grop [,targe[,name [,args [,kwargs]]]]])Create subprocess object
            //Pass in parameters,
            target=:The target point indicates that the newly created subprocess function is in the target In the function body pointed to
            args: towards target The function pointed to passes variable parameters. Note that if a single value is passed here, a comma needs to be added, because it is added in the way of a primitive
            kwargs:towards target The function body pointed to passes the key parameter, which is encapsulated in the form of a dictionary 
        2.Common properties and methods of process objects
            -Process.name :View the name of the subprocess
            -Process.pid :View the id
            -Process.start():Open subprocess
            -Process.is_alive(): Determine whether the subprocess is alive
            -Process.join(timeout = 2):Block the main process and unblock it when the sub process is finished
                                        //Subsequent code in execution
                                    timeout = 2 Block the main process for 2 seconds at most, and then unblock
            -process. terminate()Terminate the process

    4.Global variables are not shared between processes
        //Creating a child process is equivalent to copying a copy of the parent process at the same time. Some resources, including global variables, are self-contained
        //Therefore, global variables are not shared in the process  

Note:
1. The subprocesses of the process pool are all created by the main process, and they are all daemons by default. If you need to finish executing the process pool
The task of a subprocess must be: pool.close, pool.join(). Only when the process pool is closed and no new task request is received can the code of the subprocess be executed

2. In the process pool, if you use queue to create a process, you need to use multiprocessing.Manager().Queue(), otherwise there will be problems

Keywords: Python Linux

Added by alluoshi on Tue, 31 Dec 2019 14:59:19 +0200