Python学习-day10 进程

学习完线程,学习进程

进程和线程的语法有很多一样的地方,不过在操作系统中的差别确实很大。

模块是threading 和 multiprocessing

多进程multiprocessing

multiprocessing is a package that supports spawning processes using an API similar to the threading module. The multiprocessing package offers both local and remote concurrency, effectively side-stepping the Global Interpreter Lock by using subprocesses instead of threads. Due to this, the multiprocessing module allows the programmer to fully leverage multiple processors on a given machine. It runs on both Unix and Windows.

如何启动多进程

#Authon Ivor
from multiprocessing import Process
import os

def info(title):
    print(title)
    print('module name:', __name__)
    print('parent process:', os.getppid())
    print('process id:', os.getpid())
    print("")

def f(name):
    info('\033[31;1mfunction f\033[0m')
    print('hello', name)

if __name__ == '__main__':
    info('\033[32;1mmain process line\033[0m')
    p = Process(target=f, args=('bob',))
    p.start()
    p.join()
View Code


进程间通信的三种方式

Queue

#Author:Ivor
from multiprocessing import Process,Queue
import os
# threading.queue.Queue()
def run(q):
    q.put("---in the Child process---")
    print("parent_pid:",os.getppid())
    print("current_pid:",os.getpid())
    print("------------------")


if __name__ == '__main__':
    q = Queue()
    print("---main process---")
    print("parent_pid:",os.getppid())
    print("current_pid:",os.getpid())
    print("------------------")
    p = Process(target=run,args=(q,))
    p.start()
    print(q.get())
    p.join()
View Code

Pipe

#Author:Ivor
from multiprocessing import Process,Pipe

def run(conn):
    conn.send("from child")
    conn.close()

if __name__ == '__main__':
    parent_conn,child_conn = Pipe()
    p = Process(target=run,args=(child_conn,))
    p.start()
    print(parent_conn.recv())
    p.join()
View Code

Manager

#Author:Ivor
from multiprocessing import Process,Manager
import os
def run(d,l):
    d[os.getpid()] = os.getpid()
    l.append(os.getpid())
    print(l)

pro_list = []
if __name__ == '__main__':
    manager = Manager()
    d = manager.dict()
    l = manager.list()
    for i in range(10):
        p = Process(target=run,args=(d,l))
        pro_list.append(p)
        p.start()
    for i in pro_list:
        i.join()
    print(d)
    print(l)
View Code


进程池的概念

Pool

#Authon Ivor
from multiprocessing import Process,Pool
import time,os
def run(n):
    print("Process %s is running.." % n)
    time.sleep(1)
    return os.getpid()

def bar(arg):
    print("exec done---",arg)

result = []
if __name__ == '__main__':
    pool = Pool(processes=2)
    for n in range(10):
        result.append(pool.apply_async(func=run,args=(n,),callback=bar))
    for res in result:
        print("res---",res.get())
    pool.close()
    pool.join()
View Code

 

posted on 2017-04-20 14:26  DarkSugar  阅读(136)  评论(0编辑  收藏  举报

导航