Multitasking - inter-process communication and process

Procedures and processes of the difference between:

Only one program, the process can have more!

Processes have the resources, the process is the smallest unit of resource allocation!

 

Multitasking - the process of early experience:

import time
import multiprocessing


def test01():
    while True:
        print("{:=^20}".format(1))
        time.sleep(1)

def test02():
    while True:
        print("{:=^20}".format(2))
        time.sleep(1)

if __name__ == '__main__':
    process1 = multiprocessing.Process(target=test01)
    process2 = multiprocessing.Process(target=test02)

    process1.start()
    process2.start()
View Code

 

Contrast processes and threads and their advantages and disadvantages:

Compared:

The process is the smallest unit of resource allocation, the thread is the ultimate execution units.

Wire thread can not be executed independently, it must exist in accordance with the process

 

Advantages and disadvantages:

Threads and processes have advantages and disadvantages in the use: small thread execution overhead, but not conducive to the management and protection of resources; and the process contrary.

General development, like using a multi-thread!

Interprocess communication:

In fact, socket is an inter-process communication way!

By file on disk can also achieve inter-process communication!

 

Below that is the interprocess communication through the queue !

A process to write data directly to memory, another process data read from the memory summary! (In this way, neither by the network, useless disk file )

This memory is characterized by: FIFO, so called queue completed by inter-process communication.

 

Queue to put data through put, by reading data from the Queue get,

  Note : When the Queue is full, put not put into (blocking), when the Queue hollow, get out (blocking)

 

Queue schematic

 

Interprocess communication:

Import multiprocessing
 Import Time 

DEF download_from_web (Q):
     " Analog to download data from the Internet " 
    Data = List ([11,22,33,44 ]) 

    # write data to the queue 
    for TEMP in Data: 
        q.put (TEMP) 
    Print ( " {:} = ^ 20 is " .format ( " the download is complete, and is placed in the queue " )) 

DEF analysis_data (Q):
     " analog data processing " 
    worked_data = List ()
     the while True:
         Print ( " Receiving .. ." ) 
        The time.sleep ( 2 ) 
        Data = q.get () 
        worked_data.append (Data + 1'd ) 

        Print (worked_data)
         IF q.empty ():
             Print ( " acceptance completion " )
             BREAK 
DEF main ():
     # . 1, creating a queue 
    Q = multiprocessing.Queue ()   # max the hardware on condition 

    # 2, the queue as a reference to the argument transmitted in two sub-processes 
    Process1 = multiprocessing.Process (target = download_from_web, args = (Q,) ) 
    Process2 = multiprocessing.Process (target = analysis_data, args =(q,))

    process1.start()
    process2.start()


if __name__ == '__main__':
    main()
Queue achieve inter-process communication through - First Experience Demo

 

Guess you like

Origin www.cnblogs.com/zach0812/p/11413740.html