码迷,mamicode.com
首页 > 编程语言 > 详细

python 多进程

时间:2018-07-02 00:09:11      阅读:162      评论:0      收藏:0      [点我收藏+]

标签:inf   http   __init__   ima   start   ctime   ...   end   elf   

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.进程池

技术分享图片

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 多进程

标签:inf   http   __init__   ima   start   ctime   ...   end   elf   

原文地址:https://www.cnblogs.com/tsgxj/p/9251710.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!