python process small notes (1)

Process: run a program activity having individual functions on a data set. The implementation of a collection of a bunch of resources, the process must be based on the thread.

  The process must have at least one thread.

The difference between processes and threads?

(1) threads share memory space, memory processes are independent 
(2) direct communication between threads of the same process, two processes want to communicate, must be achieved through an intermediate proxy
(3) create a new thread is very simple, create a new process needs to be a clone of the parent process
(4) a thread can control and operation of the other threads in the same process, but the process can only operate child process
(5) may affect the operation of the other threads to modify the main thread, changes to a parent of the child process will not affect the

process of knowledge:
create (1) a process of
  creating a multi-threaded, single-threaded simply remove the for statement
import multiprocessing,threading,os
import time
def run(name):
    time.sleep(2)
    print("****************************************")
    print("module name :",__name__)
    print("parent process :", os.getppid())
    print("process id :", os.getpid())
    print("process info:",multiprocessing.current_process())
    print("threading info:",threading.current_thread())#进程里的主线程

    print("[%s] is process"%name)#进程里创建子线程
    print("****************************************")
    t = threading.Thread(target=handle , args=(name ,))
    t.start()

def handle(name):
    print("[%s] is thread" % name)
    print(threading.get_ident())

if __name__ =='__main__':

    run('main')
    for i in range(10):
        p = multiprocessing.Process(target=run , args=(i,))
        p.start()

 

Communication between the (2) process 
  A, process queue

    It is essentially two different lists, because when the child process created a copy of the parent process, the child process underlying a pickle in the sequence of q in place after a two processes can be met, and then Father deserialization process out, enabling interactive data. Not modify the same data, just completed the transfer of data.

import multiprocessing


def run(q):
   q.put([42,None,'leo'])
   print("child process :" , id(q))

if __name__ =="__main__":
    q = multiprocessing.Queue()
    p = multiprocessing.Process(target=run , args=(q,))
    p.start()
    print(q.get())
    print("parent process :", id(q))
  B, the pipe pies
import multiprocessing
def run(conn):
    
    while True:
        date = conn.recv()
        print("sub process:%s\n"%date)
        conn.send(date)


if __name__ =="__main__":
    parent_conn,sub_conn = multiprocessing.Pipe()
    p = multiprocessing.Process(target=run , args= (sub_conn,))
    p.start()
    while True:
        com = input("from parent>>\n:")
        parent_conn.send(com)
        print("parent process :%s"%parent_conn.recv())    
  C、Manage
import multiprocessing,os

def run(i,d,l,lock):
    lock.acquire()   #锁
    d[os.getpid()] = i
    # l.append(os.getpid())
    for a in range(1000):
        l[0] +=1
    print(l)
    lock.release()

if __name__ =="__main__":
    with multiprocessing.Manager() as manager:
    # 相当于 manager=multiprocessing.Manager()

        d = manager.dict()#生成一个字典,可在多个进程间共享和传递
        l = manager.list(range(5))
        p_list=[]
        lock =multiprocessing.Lock()#进程锁,主要作用是输出屏幕的数据不乱
        for i in range(10):
            p = multiprocessing.Process(target=run , args=(i,d,l,lock,))
            p_list.append(p)
            p.start()

        for i in p_list:  #等待子进程执行完毕
            i.join()

        print(d)
        print(l)
 
(3)进程池
  进程池:进程太多,启动时开销太大,导致系统变慢。需要限制每次启动进程的个数。
  必须先调用close()再调用join()
import multiprocessing,os,time

def run(i):
    time.sleep(2)
    print("[%s] is processing "%os.getpid())
    return i+100,'xxxx'

def Bar(*args):#主进程调用回调
    print('-->exec done:',args)
    print('callback from :',multiprocessing.current_process())

if __name__ == '__main__':
    pool = multiprocessing.Pool(processes=5)  #允许进程池里同时放入5个进程

    for i in range(10):#启动了10个进程,同一时间只有5个进程运行
        # pool.apply(func=run , args=(i,))#同步执行,串行
        #pool.apply_async(func=run, args=(i,))  # 异步执行,并行
        pool.apply_async(func=run, args=(i,),callback=Bar)  # 异步执行,并行,callback回调


    print('end')
    pool.close()
    pool.join()#进程池中进程执行完毕后再关闭,如果注释,那么程序直接关闭。

  

 
 

Guess you like

Origin www.cnblogs.com/gtq7512/p/11375375.html