一、多进程

1、进程模块 multiprocessing

简单的创建一个进程

#!/usr/bin/env python
# -*- coding: utf- -*-
# @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()

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('\033[31;1m called from child process function f \033[0m')#打印f函数的父子进程
print('hello ',name) if __name__ == '__main__':
info('\033[32;1m main process \033[0m') #主程序调用info函数打印父子进程
p = Process(target=f,args=('hanmeimei',)) #主程序启动一个子进程,打印子进程的父子函数
p.start()
p.join()
第一次
main process
module name: __main__
parent process name:
child process name: called from child process function f
module name: __mp_main__
parent process name:
child process name:
hello hanmeimei 第二次
main process
module name: __main__
parent process name:
child process name: called from child process function f
module name: __mp_main__
parent process name:
child process name:
hello hanmeimei 第三次
main process
module name: __main__
parent process name:
child process name:
called from child process function f
module name: __mp_main__
parent process name:
child process name:
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('',mq.get_nowait())#获取队列元素
print('',mq.get_nowait())

Queue

in child before cq.put:
fome main
['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(): #启动多个子进程
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) #打印最终的列表
{: } []
{: , : } [, ]
{: , : , : } [, , ]
{: , : , : , : } [, , , ]
{: , : , : , : , : } [, , , , ]
{: , : , : , : , : , : } [, , , , , ]
{: , : , : , : , : , : , : } [, , , , , , ]
{: , : , : , : , : , : , : , : } [, , , , , , , ]
{: , : , : , : , : , : , : , : , : } [, , , , , , , , ]
{: , : , : , : , : , : , : , : , : , : } [, , , , , , , , , ]
{: , : , : , : , : , : , : , : , : , : }
[, , , , , , , , , ]

结果

python运维开发之第十天的更多相关文章

  1. Python运维开发基础09-函数基础【转】

    上节作业回顾 #!/usr/bin/env python3 # -*- coding:utf-8 -*- # author:Mr.chen # 实现简单的shell命令sed的替换功能 import ...

  2. Python运维开发基础10-函数基础【转】

    一,函数的非固定参数 1.1 默认参数 在定义形参的时候,提前给形参赋一个固定的值. #代码演示: def test(x,y=2): #形参里有一个默认参数 print (x) print (y) t ...

  3. Python运维开发基础08-文件基础【转】

    一,文件的其他打开模式 "+"表示可以同时读写某个文件: r+,可读写文件(可读:可写:可追加) w+,写读(不常用) a+,同a(不常用 "U"表示在读取时, ...

  4. Python运维开发基础07-文件基础【转】

    一,文件的基础操作 对文件操作的流程 [x] :打开文件,得到文件句柄并赋值给一个变量 [x] :通过句柄对文件进行操作 [x] :关闭文件 创建初始操作模板文件 [root@localhost sc ...

  5. Python运维开发基础06-语法基础【转】

    上节作业回顾 (讲解+温习120分钟) #!/usr/bin/env python3 # -*- coding:utf-8 -*- # author:Mr.chen # 添加商家入口和用户入口并实现物 ...

  6. Python运维开发基础05-语法基础【转】

    上节作业回顾(讲解+温习90分钟) #!/usr/bin/env python # -*- coding:utf-8 -*- # author:Mr.chen import os,time Tag = ...

  7. Python运维开发基础04-语法基础【转】

    上节作业回顾(讲解+温习90分钟) #!/usr/bin/env python3 # -*- coding:utf-8 -*- # author:Mr.chen # 仅用列表+循环实现“简单的购物车程 ...

  8. Python运维开发基础03-语法基础 【转】

    上节作业回顾(讲解+温习60分钟) #!/usr/bin/env python3 # -*- coding:utf-8 -*- # author:Mr.chen #只用变量和字符串+循环实现“用户登陆 ...

  9. Python运维开发基础02-语法基础【转】

    上节作业回顾(讲解+温习60分钟) #!/bin/bash #user login User="yunjisuan" Passwd="666666" User2 ...

随机推荐

  1. POJ1182 食物链(并查集)

    食物链 Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 55260   Accepted: 16210 Description ...

  2. poj1013

    题目大意:假造的银币 Sally Jones有一些游客给的银币,但是只有11枚是真正的银币(有一枚是假的),从颜色和大小是无法区分真比还是假币的,但是它的重量和真币是不同的,Sally Jones它是 ...

  3. C#中的 IList, ICollection ,IEnumerable 和 IEnumerator

    IList, ICollection ,IEnumerable 很显然,这些都是集合接口的定义,先看看定义: // 摘要: // 表示可按照索引单独访问的对象的非泛型集合. [ComVisible(t ...

  4. 一步一步学android之事件篇——单击事件

    在使用软件的时候单击事件必不可少,比如我想确定.取消等都需要用户的单击,所有的单击事件都是由View.OnClickListener接口来进行处理的,接口定义如下: public static int ...

  5. javaweb笔记4之httpservlet

    1 httpservlet简介 service方法是Servlet的入口方法,调用servlet会首先调用service方法.在service方法中,会根据请求方式分别调用不同的doXXX方法.例如, ...

  6. 必胜宅急送Web app设计背后的思考

    O2O模式是餐饮业在移动消费趋势下主动拥抱互联网的方向,迎合餐饮消费者从以往经验判断为主转变为依靠移动设备.lbs.社交网络进行立体决策的过程.继App客户端之后,手机web app也逐渐成为O2O中 ...

  7. Meth | elementary OS常用配置

    一,搜狗输入法 sudo apt-get remove ibussudo add-apt-repository ppa:fcitx-team/nightlysudo apt-get updatesud ...

  8. [转] doxygen使用总结

    doxygen [功能] 为许多种语言编写的程序生成文档的工具. [举例] *生成一个模板配置文件,模板文件中有详细的注释: $doxgen -g test 这样,会生成一个test文件,1500多行 ...

  9. iOS 如何优雅的处理“回调地狱Callback hell”(一) (上)

    前言 最近看了一些Swift关于封装异步操作过程的文章,比如RxSwift,RAC等等,因为回调地狱我自己也写过,很有感触,于是就翻出了Promise来研究学习一下.现将自己的一些收获分享一下,有错误 ...

  10. 各种开发语言示例调用HTTP接口(示例中默认HTTP接口编码为gb2312)

    asp示例: function getHTTPPage(strurl,data)   on error resume next   set http = Server.CreateObject(&qu ...