python multiprocessing 使用
如何在Pool中使用Queue,Stack Overflow的回答,戳这里
其实吧官方文档看一遍应该就大部分都懂了。
需要注意的是:在使用多进程的时候,我们的进程函数的传入参数必须是pickle-able的,也就是参数必须可以被pickle保存下来,multiprocessing.Queue对象不能传递给pool.apply_*()等函数,需要使用multiprocessing.Manager().Queue()产生的对象
贴一下代码
# -*- coding: UTF-8 -*-
from multiprocessing import Process, Pool, Queue, Manager, JoinableQueue
import time
import os res = [] def put_task():
msg = []
for i in xrange(50):
time.sleep(0.1)
msg.append(str(os.getpid()))
return ','.join(msg) def collect_results(result):
res.append(result) def take_task(queue):
while 1:
print(queue.get(True)) def task_put(name, que):
for i in range(10):
time.sleep(1)
que.put("%d is done" % name) def task_take_queue(que, n):
i = 0
while i < n:
print(que.get(True))
i += 1 def consumer(input_q): while True:
item = input_q.get(True)
# 处理项目
print item # 此处替换为有用的工作
# 发出信号通知任务完成
input_q.task_done() def producer(output_q):
sequence = [1, 2, 3, 4] # range(5)[1:5]
for item in sequence:
# 将项目放入队列
time.sleep(1)
output_q.put(item)
# 建立进程 def method_1():
pool = Pool()
res = pool.map_async(put_task, range(5))
pool.close()
pool.join()
from pprint import pprint
pprint(res.get()) def method_2():
pool = Pool()
pool.apply_async(put_task, callback=collect_results)
pool.apply_async(put_task, callback=collect_results)
pool.apply_async(put_task, callback=collect_results)
pool.close()
pool.join()
from pprint import pprint
pprint(res) def method_3():
pool = Pool(processes=10)
m = Manager()
q = m.Queue()
for i in range(5):
pool.apply_async(task_put, (i, q))
pool.apply_async(task_take_queue, (q, 50))
pool.close()
pool.join() def method_4():
q = JoinableQueue() # 运行使用者进程
cons_p = Process(target=consumer, args=(q,))
cons_p.daemon = True # 定义该进程为后台运行 True - When a process exits, it attempts to terminate all of its daemonic child processes.
cons_p.start()
# 生产项目,sequence代表要发送给使用者的项目序列
# 在时间中,这可能是生成器的输出或通过一些其他方式生产出来 producer(q)
# 等待所有项目被处理
q.join() if __name__ == '__main__':
method_4()
import multiprocessing
import os
import time def pool_init(q):
global queue # make queue global in workers
queue = q def task():
# can use `queue` here if you like
for i in range(5):
time.sleep(1)
queue.put(os.getpid()) def take_task():
while 1:
print(queue.get(True)) def run(pool):
tasks = []
tasks.append(pool.apply_async(take_task))
for i in range(os.cpu_count()):
tasks.append(pool.apply_async(task))
for t in tasks:
print(t.get(), ) if __name__ == '__main__':
queue = multiprocessing.Queue()
pool = multiprocessing.Pool(initializer=pool_init, initargs=(queue,))
run(pool)
pool.close()
pool.join()
python multiprocessing 使用的更多相关文章
- python multiprocessing example
python multiprocessing example Server Code: #!/usr/bin/python #-*- coding: UTF-8 -*- # mpserver.py # ...
- python MultiProcessing模块进程间通信的解惑与回顾
这段时间沉迷MultiProcessing模块不能自拔,没办法,python的基础不太熟,因此就是在不断地遇到问题解决问题.之前学习asyncio模块学的一知半解,后来想起MultiProcessin ...
- python multiprocessing模块
python multiprocessing模块 原文地址 multiprocessing multiprocessing支持子进程.通信和共享数据.执行不同形式的同步,提供了Process.Queu ...
- python multiprocessing.Process
在使用Kafka-python时自己写的一个bug 我在一个进程的__init__中初始化了一个producer,但是一直不好用 但是在函数里直接new一个就好用了 why? 需要说明的是produc ...
- python MultiProcessing标准库使用Queue通信的注意要点
今天原本想研究下MultiProcessing标准库下的进程间通信,根据 MultiProcessing官网 给的提示,有两种方法能够来实现进程间的通信,分别是pipe和queue.因为看queue顺 ...
- Python multiprocessing
 推荐教程 官方文档 multiprocess各个模块较详细介绍 廖雪峰教程--推荐 Pool中apply, apply_async的区别联系 (推荐)python多进程的理解 multiproce ...
- python multiprocessing深度解析
在写python多线程代码的时候,会用到multiprocessing这个包,这篇文章总结了一些这个包在多进程管理方面的一些原理和代码分析. 1. 问题一:是否需要显式调用pool的close和joi ...
- Python multiprocessing模块的Pool类来代表进程池对象
#-*-coding:utf-8-*- '''multiprocessing模块提供了一个Pool类来代表进程池对象 1.Pool可以提供指定数量的进程供用户调用,默认大小是CPU的核心数: 2.当有 ...
- python Multiprocessing 多进程应用
在运维工作中,经常要处理大量数据,或者要跑一些时间比较长的任务,可能都需要用到多进程,不管是管理端下发任务,还是客户端执行任务,如果服务器配置还可以,跑多进程还是挺能解决问题的 Multiproces ...
- python multiprocessing.Pool 中map、map_async、apply、apply_async的区别
multiprocessing是python的多进程库,multiprocessing.dummy则是多线程的版本,使用都一样. 其中都有pool池的概念,进程池/线程池有共同的方法,其中方法对比如下 ...
随机推荐
- sql视图中写case判断null值
下面是正解 用 is null (case when dbo.Feedback.Funnel is null then '否' when dbo.Feedback.Funnel='否' then '是 ...
- 扩展jquery插件的方式
- Java利用反射取得类的所有信息
Java中可以利用反射获取类的名称.构造函数.属性.方法.也就是说可以通过反射可以取得类的所有信息(不管该成员是否封装为private). 如有下面的Dept类定义: package org.lyk. ...
- iOS调用系统页面中文显示
在开发的过程中,我们会接入很多的sdk,比如相机,相册,是否允许获取位置等,大多数的情况下是默认显示英文, 在plist文件里面添加一个key就可以了,如下图: key:Localization na ...
- php-fpm.conf 重要参数 max_children 和 request_terminate_timeout
php-fpm.conf 重要参数 max_children 和 request_terminate_timeout php-fpm.conf有两个至关重要的参数:一个是”max_children”, ...
- Linux学习7-Linux常用命令(3)
文件处理命令 命令名称:touch 命令英文原意:touch 命令所在路径:/bin/touch 执行权限:所有用户 功能描述:创建空文件 语法:touch[文件名] 范例: $tou ...
- SQL0286N 找不到页大小至少为 "8192"、许可使用授权标识 "db2inst" 的缺省表空间。
在 SQL 处理期间,它返回: SQL0286N 找不到页大小至少为 "8192".许可使用授权标识 "db2inst" 的缺省表空间. 顾名思义,DB2默认 ...
- python学习之老男孩python全栈第九期_day015知识点总结
# 作用域相关(2)locals() # 返回本地作用域中的所有名字 globals() # 返回全局作用域中的所有名字 # 迭代器/生成器相关(3)range()print('__next__' i ...
- listview更改选中时item背景色(转)
默认情况下使用ListView背景色是黑色,选中item的高亮颜色是菊黄色,很多时候不得不自己定义背景色或者背景图 android:cacheColorHint="@android:colo ...
- 转:PHP中的使用curl发送请求(GET请求和POST请求)
原文地址:http://www.jb51.net/article/104974.htm 使用CURL发送请求的基本流程 使用CURL的PHP扩展完成一个HTTP请求的发送一般有以下几个步骤: 1.初始 ...