python运维开发之第十天

一、多进程

1、进程模块 multiprocessing

简单的创建一个进程

#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Author  : Willpower-chen

from multiprocessing import Process
def run(name):
    print('my name is %s'% name)
if __name__ == '__main__':
    p = Process(target=run,args=('lilei',))#创建一个进程实例
    p.start()
View Code

2、进程号获取,父子进程关系

from multiprocessing import Process
import os

def info(title):    #info 函数打印父子进程
    print(title)
    print('module name:',__name__)
    print('parent process name:',os.getppid())#打印父进程
    print('child process name:',os.getpid())#打印子进程

def f(name):
    info('33[31;1m called from child process function f 33[0m')#打印f函数的父子进程
    print('hello ',name)

if __name__ == '__main__':
    info('33[32;1m main process 33[0m')     #主程序调用info函数打印父子进程
    p = Process(target=f,args=('hanmeimei',))        #主程序启动一个子进程,打印子进程的父子函数
    p.start()
    p.join()
View Code
第一次
 main process 
module name: __main__
parent process name: 2844
child process name: 6968

 called from child process function f 
module name: __mp_main__
parent process name: 6968
child process name: 6728
hello  hanmeimei

第二次
 main process 
module name: __main__
parent process name: 2844
child process name: 5824

 called from child process function f 
module name: __mp_main__
parent process name: 5824
child process name: 5868
hello  hanmeimei

第三次
 main process 
module name: __main__
parent process name: 2844
child process name: 5776
 called from child process function f 
module name: __mp_main__
parent process name: 5776
child process name: 5952
hello  hanmeimei
结果

结论:pycharm程序运行的父进程是win7任务栏里的pycharm进程号,每一个进程都会有一个父进程。父进程os.getppid(),子进程os.getpid()

3、进程间通信Queue,pipe

(1)Queue

from multiprocessing import Queue,Process

def f(cq):
    print('in child before cq.put:',cq.qsize()) #子进程put前查看队列中是否有数据
    cq.put(['my','name','is',['lilei','xixi']]) #往队列中添加一个元素

if __name__ == '__main__':
    mq = Queue()            #定义进程队列实例
    mq.put('fome main')     #往队列中添加一个元素
    p = Process(target=f,args=(mq,))#创建一个子进程,并将mq传给子进程
    p.start()                       #启动
    p.join()                        #等待子进程执行完毕
    print('444',mq.get_nowait())#获取队列元素
    print('444',mq.get_nowait())
Queue
in child before cq.put: 1
444 fome main
444 ['my', 'name', 'is', ['lilei', 'xixi']]
结果

(2)pipe

from multiprocessing import  Process,Pipe

def f(conn):
    conn.send("from child1") #发送数据
    conn.send("from child2") #发送数据
    print('client recv:',conn.recv())#接收数据
    conn.close()

if __name__ == '__main__':
    a_conn, b_conn = Pipe()     #创建管道
    p = Process(target=f,args=(b_conn,))    #实例化子进程,函数f,参数管道的一端
    p.start()
    print(a_conn.recv())
    print(a_conn.recv())
    a_conn.send('from parent') #父进程发送数据
    p.join()
Pipe

3、进程间数据共享manager

from multiprocessing import Process,Manager
import os
def run(d,l):
    d[os.getpid()] = os.getpid()   #以当前子进程的pid为key,同时pid也作为value
    l.append(os.getpid())           
    print(d,l)

if __name__ == '__main__':
        with Manager() as manager:
            d = manager.dict()  #manager 字典
            l = manager.list()  #manager 列表
            p_list = []         #空的列表,为之后的添加进程实例
            for i in range(10): #启动多个子进程
                p = Process(target=run,args=(d,l))#起一子进程,执行run参数d,l
                p.start()
                p_list.append(p)  #添加进程实例至列表
            for r in p_list:    #循环子进程
                r.join()          #等待子进程结束
            print(d)            #打印最终的字典
            print(l)            #打印最终的列表
View Code 
{3604: 3604} [3604]
{6992: 6992, 3604: 3604} [3604, 6992]
{6992: 6992, 3752: 3752, 3604: 3604} [3604, 6992, 3752]
{6992: 6992, 3752: 3752, 3604: 3604, 3056: 3056} [3604, 6992, 3752, 3056]
{6992: 6992, 3752: 3752, 3604: 3604, 3056: 3056, 796: 796} [3604, 6992, 3752, 3056, 796]
{6992: 6992, 3056: 3056, 3604: 3604, 3752: 3752, 796: 796, 5856: 5856} [3604, 6992, 3752, 3056, 796, 5856]
{6992: 6992, 3056: 3056, 3604: 3604, 3752: 3752, 4748: 4748, 796: 796, 5856: 5856} [3604, 6992, 3752, 3056, 796, 5856, 4748]
{6992: 6992, 3056: 3056, 3604: 3604, 3752: 3752, 4748: 4748, 796: 796, 5856: 5856, 6684: 6684} [3604, 6992, 3752, 3056, 796, 5856, 4748, 6684]
{6992: 6992, 3056: 3056, 3604: 3604, 3752: 3752, 4748: 4748, 796: 796, 5856: 5856, 6684: 6684, 6432: 6432} [3604, 6992, 3752, 3056, 796, 5856, 4748, 6684, 6432]
{6992: 6992, 3056: 3056, 3604: 3604, 5356: 5356, 3752: 3752, 4748: 4748, 796: 796, 5856: 5856, 6684: 6684, 6432: 6432} [3604, 6992, 3752, 3056, 796, 5856, 4748, 6684, 6432, 5356]
{6992: 6992, 3056: 3056, 3604: 3604, 5356: 5356, 3752: 3752, 4748: 4748, 796: 796, 5856: 5856, 6684: 6684, 6432: 6432}
[3604, 6992, 3752, 3056, 796, 5856, 4748, 6684, 6432, 5356]
结果
原文地址:https://www.cnblogs.com/willpower-chen/p/5912339.html