简单谈谈python中的多进程


Posted in Python onNovember 06, 2016

进程是由系统自己管理的。

1:最基本的写法

from multiprocessing import Pool

def f(x):
  return x*x

if __name__ == '__main__':
  p = Pool(5)
  print(p.map(f, [1, 2, 3]))
[1, 4, 9]

2、实际上是通过os.fork的方法产生进程的

unix中,所有进程都是通过fork的方法产生的。

multiprocessing Process
os

info(title):
  title
  , __name__
  (os, ): , os.getppid()
  , os.getpid()

f(name):
  info()
  , name

__name__ == :
  info()
  p = Process(=f, =(,))
  p.start()
  p.join()

3、线程共享内存

threading

run(info_list,n):
  info_list.append(n)
  info_list

__name__ == :
  info=[]
  i ():
    p=threading.Thread(=run,=[info,i])
    p.start()
[0]
[0, 1]
[0, 1, 2]
[0, 1, 2, 3]
[0, 1, 2, 3, 4]
[0, 1, 2, 3, 4, 5]
[0, 1, 2, 3, 4, 5, 6]
[0, 1, 2, 3, 4, 5, 6, 7]
[0, 1, 2, 3, 4, 5, 6, 7, 8]
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]

进程不共享内存:

multiprocessing Process
run(info_list,n):
  info_list.append(n)
  info_list

__name__ == :
  info=[]
  i ():
    p=Process(=run,=[info,i])
    p.start()
[1]
[2]
[3]
[0]
[4]
[5]
[6]
[7]
[8]
[9]

若想共享内存,需使用multiprocessing模块中的Queue

multiprocessing Process, Queue
f(q,n):
  q.put([n,])

__name__ == :
  q=Queue()
  i ():
    p=Process(=f,=(q,i))
    p.start()
  :
    q.get()

4、锁:仅是对于屏幕的共享,因为进程是独立的,所以对于多进程没有用

multiprocessing Process, Lock
f(l, i):
  l.acquire()
  , i
  l.release()

__name__ == :
  lock = Lock()

  num ():
    Process(=f, =(lock, num)).start()
hello world 0
hello world 1
hello world 2
hello world 3
hello world 4
hello world 5
hello world 6
hello world 7
hello world 8
hello world 9

5、进程间内存共享:Value,Array

multiprocessing Process, Value, Array

f(n, a):
  n.value = i ((a)):
    a[i] = -a[i]

__name__ == :
  num = Value(, )
  arr = Array(, ())

  num.value
  arr[:]

  p = Process(=f, =(num, arr))
  p.start()
  p.join()
0.0
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
3.1415927
[0, -1, -2, -3, -4, -5, -6, -7, -8, -9]

#manager共享方法,但速度慢

multiprocessing Process, Manager

f(d, l):
  d[] = d[] = d[] = l.reverse()

__name__ == :
  manager = Manager()

  d = manager.dict()
  l = manager.list(())

  p = Process(=f, =(d, l))
  p.start()
  p.join()

  d
  l
# print '-------------'这里只是另一种写法
# print pool.map(f,range(10))
{0.25: None, 1: '1', '2': 2}
[9, 8, 7, 6, 5, 4, 3, 2, 1, 0]

#异步:这种写法用的不多

multiprocessing Pool
time
f(x):
  x*x
  time.sleep()
  x*x

__name__ == :
  pool=Pool(=)
  res_list=[]
  i ():
    res=pool.apply_async(f,[i])  res_list.append(res)

  r res_list:
    r.get(timeout=10) #超时时间

同步的就是apply

Python 相关文章推荐
python小技巧之批量抓取美女图片
Jun 06 Python
Windows下Python使用Pandas模块操作Excel文件的教程
May 31 Python
详解Python中的静态方法与类成员方法
Feb 28 Python
Python正则表达式知识汇总
Sep 22 Python
wxPython的安装图文教程(Windows)
Dec 28 Python
Python实现嵌套列表去重方法示例
Dec 28 Python
Python3.6日志Logging模块简单用法示例
Jun 14 Python
pytorch 调整某一维度数据顺序的方法
Dec 08 Python
浅谈Pycharm中的Python Console与Terminal
Jan 17 Python
python 实现任务管理清单案例
Apr 25 Python
零基础小白多久能学会python
Jun 22 Python
Python爬取英雄联盟MSI直播间弹幕并生成词云图
Jun 01 Python
python自带的http模块详解
Nov 06 #Python
Python程序中设置HTTP代理
Nov 06 #Python
Python 搭建Web站点之Web服务器网关接口
Nov 06 #Python
Python 搭建Web站点之Web服务器与Web框架
Nov 06 #Python
读写json中文ASCII乱码问题的解决方法
Nov 05 #Python
django1.8使用表单上传文件的实现方法
Nov 04 #Python
Python+MongoDB自增键值的简单实现
Nov 04 #Python
You might like
PHP session会话的安全性分析
2011/09/08 PHP
PHP jQuery表单,带验证具体实现方法
2014/02/15 PHP
Android AsyncTack 异步任务实例详解
2016/11/02 PHP
Laravel 实现Controller向blade前台模板赋值的四种方式小结
2019/10/22 PHP
Using the TextRange Object
2006/10/14 Javascript
学习YUI.Ext 第二天
2007/03/10 Javascript
利用NodeJS的子进程(child_process)调用系统命令的方法分享
2013/06/05 NodeJs
jquery.validate.js插件使用经验记录
2014/07/02 Javascript
JavaScript将一个数组插入到另一个数组的方法
2015/03/19 Javascript
Vue.js基础知识汇总
2016/04/27 Javascript
谈谈target=_new和_blank的不同之处
2016/10/25 Javascript
Javascript this 函数深入详解
2016/12/13 Javascript
Jquery Easyui菜单组件Menu使用详解(15)
2016/12/18 Javascript
JS实现异步上传压缩图片
2017/04/22 Javascript
laypage+SpringMVC实现后端分页
2019/07/27 Javascript
Pycharm技巧之代码跳转该如何回退
2017/07/16 Python
python利用标准库如何获取本地IP示例详解
2017/11/01 Python
对python:循环定义多个变量的实例详解
2019/01/20 Python
python3 tkinter实现点击一个按钮跳出另一个窗口的方法
2019/06/13 Python
Python实现图片批量加入水印代码实例
2019/11/30 Python
Python count函数使用方法实例解析
2020/03/23 Python
在python中使用pyspark读写Hive数据操作
2020/06/06 Python
css3 transform导致子元素固定定位变成绝对定位的方法
2020/03/06 HTML / CSS
英国复古服装购物网站:Collectif
2019/10/30 全球购物
自荐信怎么写好
2013/11/11 职场文书
体育老师的教学自我评价分享
2013/11/19 职场文书
旷课检讨书2000字
2014/01/14 职场文书
离婚协议书怎样才有法律效力
2014/10/10 职场文书
硕士学位申请报告
2015/05/15 职场文书
2015年重阳节活动主持词
2015/07/30 职场文书
公司员工培训管理制度
2015/08/04 职场文书
安全教育主题班会总结
2015/08/14 职场文书
请学会珍惜眼前,因为人生没有下辈子!
2019/11/12 职场文书
MySQL创建定时任务
2022/01/22 MySQL
SpringBoot整合Minio文件存储
2022/04/03 Java/Android
windows server2008 开启端口的实现方法
2022/06/25 Servers