python 多进程

Posted 起个名字真难1

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了python 多进程相关的知识,希望对你有一定的参考价值。

1.进程的创建

from multiprocessing import Process
import time,os
def Hi(name):
    time.sleep(3)
    print("Hello %s" % name,time.ctime())
    print("进程号是 ",os.getpid())
if __name__=="__main__":
    for i in range(3):
        t = Process(target=Hi,args=("萌萌",))
        t.start()
    print("主进程号:",os.getpid())
    print("end  all")
View Code

 

 

2.进程的阻碍

from multiprocessing import Process
import time,os
def hello(name):
    time.sleep(2)
    print("Hello %s" % name,time.ctime())
    print("进程号是 ",os.getpid())
if __name__=="__main__":
    L=[]
    for i in range(3):
        t= Process(target=hello,args=("梦梦",))
        t.start()
        L.append(t)
    # t.join()
    # t.join() # 阻碍主进程
    for t in L:
        t.join()
    print("主进程号:",os.getpid())
    print("end  all")
View Code

 

2.2.进程的继承调用

from multiprocessing import Process
import time,os
class MyProcess(Process):
    # def __init__(self,name):
    #     super(MyProcess,self).__init__()
    #     self.name =name
    def run(self):
        time.sleep(1)
        print("hello",self.name,time.ctime())
if __name__ == \'__main__\':
    p_list=[]
    for i in range(3):
        # p =MyProcess("张三")
        p = MyProcess()
        p.start()
        p_list.append(p)
    # p.join()
    for p in p_list:
        p.join()
    print("end")
View Code

 

3.进程的守护

# ___\'\'Daemon:进程守护、子进程跟随主进程一起退\'\'\'

class myprocess:
    def music1(self):
        print("begin to listen %s" %time.ctime())
        time.sleep(2)
        print("stop to listen %s" % time.ctime())
    def game1(self):
        print("begin to game %s" % time.ctime())
        time.sleep(3)
        print("stop to game %s" % time.ctime())
if __name__ == "__main__":
    duixiang=myprocess()
    p1 = Process(target=duixiang.music1)
    p2 = Process(target=duixiang.game1)
    p2.daemon=True
    L=[]
    L.append(p1)
    L.append(p2)
    for i in L:
        i.start()
    # i.join()
    print("all over end.......")

 

4.进程通信队列-Queue

from multiprocessing import Process,Queue
import time
def foo(q):
    time.sleep(1)
    q.put(6776)
    q.put({"name":"张三"})

if __name__ == \'__main__\':
    q = Queue() 
    #q=multiprocessing.Queue()
    #创建一个进程队列
    p=Process(target=foo,args=(q,))
    p.start()
    print(q.get())
    print(q.get_nowait())
    print(q.get(block=False))

 

5.进程通信-管道Pipe

from multiprocessing import Process, Pipe
import time
import os
def f(conn):
    conn.send([12,{"name":"子豪"},"hello"])
    date=conn.recv()
    print("父亲发的消息是",date,time.ctime())
    conn.close()
    print("子进程进程号%s,子进程拿到的数据ID %s:"%(os.getpid(),id(conn)))

if __name__ == \'__main__\':
    parent_conn,child_conn=Pipe()
    p =Process(target=f,args=(child_conn,))
    p.start()
    sunsend=parent_conn.recv()
    print("儿子发消息是:",sunsend,time.ctime())
    parent_conn.send("儿子,你好")
    parent_conn.close()
    print("父进程进程号%s,父进程拿到的数据ID %s:" %(os.getpid(),id(parent_conn)))
    p.join()

 

6.进程同步

\'\'\'加锁的部分完全是串行的\'\'\'
from  multiprocessing import Process,Lock
import time
def f(l,i):
    l.acquire()
    time.sleep(1)
    print("hello " ,i)
    l.release()
if __name__ == \'__main__\':
    lock =Lock()
    for num in range(10):
        Process(target=f,args=(lock,num)).start()

 7.进程间的数据共享 Managers

# ------------------Managers 进程间数据共享-----------------
# # Queue、Pipe 只实现了数据的互交并没有实现数据的共享
from multiprocessing import Manager,Process
import os
def fun(d,L,n):
    d[n]="1" # {"0": "1"}
    d["2"]="2" # {"0": "1","2":"2"}
    L.append(n)
    print(\'子进程进程号:%s, 字典id %s,列表id %s\'%(os.getpid(),id(d),id(L)))
if __name__ == \'__main__\':
    with Manager() as manager:
        d = manager.dict()
        L = manager.list(range(9))
        for i in range(10):
            p = Process(target=fun,args=(d,L,i))
            p.start()
        p.join()
        print(d)
        print(L)
    print("所有任务都已经结束")
    # print(d)
    # print(L)

  

 

8.进程池

def f(i):
    time.sleep(1)
    print(i)
if __name__ == \'__main__\':
    pool=Pool(5)  # 不填参数默认以CPU数量运行
    for i in range(100):
        # pool.apply(func=f,args=(i,)) # 同步接口
        pool.apply_async(func=f,args=(i,))

    pool.close()
    pool.join()
    # 进程池的格式先close后join、调用顺序是固定的
    print("over all")

 

以上是关于python 多进程的主要内容,如果未能解决你的问题,请参考以下文章

[Python3] 043 多线程 简介

python中的多线程和多进程编程

代码片段:Shell脚本实现重复执行和多进程

线程学习知识点总结

什么是多线程,多进程?

多线程编程