python下multiprocessing和gevent的组合使用
python下multiprocessing和gevent的组合使用
对于有些人来说Gevent和multiprocessing组合在一起使用算是个又高大上又奇葩的工作模式.
Python的多线程受制于GIL全局锁的特性,Gevent身为协程也是线程的一种,只是io调度上自己说了算而已。
那么如何使用多个cpu核心? 可以利用多进程mutliprocessing来进行多核并行工作,在多进程里面使用gevent协程框架可以更好的做io调度,相比线程来说减少了无谓的上下文切换.
废话少说,直接上个例子. 下面是多进程下生产者消费者的工作模式,代码本身很简单,自己跑一下就知道怎么一回事了.
# blog: xiaorui.cc from multiprocessing import Process, cpu_count, Queue, JoinableQueue
from gevent import monkey; monkey.patch_all();
import gevent
import datetime
from Queue import Empty class Consumer(object):
def __init__(self, q, no_tasks, name):
self._no_tasks = no_tasks
self._queue = q
self.name = name
self._rungevent(self._queue, self._no_tasks) def _rungevent(self, q, no_tasks):
jobs = [gevent.spawn(self._printq) for x in xrange(no_tasks)]
gevent.joinall(jobs) def _printq(self):
while 1:
value = self._queue.get()
if value is None:
self._queue.task_done()
break
else:
print("{0} time: {1}, value: {2}".format(self.name, \
datetime.datetime.now(), value))
return class Producer(object):
def __init__(self, q, no_tasks, name, consumers_tasks):
print(name)
self._q = q
self._no_tasks = no_tasks
self.name = name
self.consumer_tasks = consumers_tasks
self._rungevent() def _rungevent(self):
jobs = [gevent.spawn(self.produce) for x in xrange(self._no_tasks)]
gevent.joinall(jobs)
for x in xrange(self.consumer_tasks):
self._q.put_nowait(None)
self._q.close() def produce(self):
for no in xrange(100):
print no
self._q.put(no, block=False)
return def main():
total_cores = cpu_count()
total_processes = total_cores * 2
q = JoinableQueue()
print("Gevent on top multiprocessing with 17 gevent coroutines 10 producers gevent and 7 consumers gevent")
producer_gevents = 10
consumer_gevents = 7
jobs = []
start = datetime.datetime.now()
for x in xrange(total_processes):
if not x % 2:
p = Process(target=Producer, args=(q, producer_gevents, "producer %d" % x, consumer_gevents))
p.start()
jobs.append(p)
else:
p = Process(target=Consumer, args=(q, consumer_gevents, "consumer %d" % x))
p.start()
jobs.append(p) for job in jobs:
job.join() print("{0} process with {1} producer gevents and {2} consumer gevents took{3}\
seconds to produce {4} numbers and consume".format(total_processes, \
producer_gevents * total_cores,
consumer_gevents * total_cores, \
datetime.datetime.now() - start,
producer_gevents * total_cores * 100)) if __name__ == '__main__':
main()
test
python下multiprocessing和gevent的组合使用的更多相关文章
- python3下multiprocessing、threading和gevent性能对比----暨进程池、线程池和协程池性能对比
python3下multiprocessing.threading和gevent性能对比----暨进程池.线程池和协程池性能对比 标签: python3 / 线程池 / multiprocessi ...
- python 协程库gevent学习--gevent数据结构及实战(三)
gevent学习系列第三章,前面两章分析了大量常用几个函数的源码以及实现原理.这一章重点偏向实战了,按照官方给出的gevent学习指南,我将依次分析官方给出的7个数据结构.以及给出几个相应使用他们的例 ...
- python中multiprocessing.pool函数介绍_正在拉磨_新浪博客
python中multiprocessing.pool函数介绍_正在拉磨_新浪博客 python中multiprocessing.pool函数介绍 (2010-06-10 03:46:5 ...
- python的multiprocessing模块进程创建、资源回收-Process,Pool
python的multiprocessing有两种创建进程的方式,每种创建方式和进程资源的回收都不太相同,下面分别针对Process,Pool及系统自带的fork三种进程分析. 1.方式一:fork( ...
- Python下划线简介
Python中下划线的5种含义 分享一篇文章:The Meaning of Underscores in Python. 本文介绍了Python中单下划线和双下划线("dunder" ...
- python下划线的5种含义
本文介绍了Python中单下划线和双下划线("dunder")的各种含义和命名约定,名称修饰(name mangling)的工作原理,以及它如何影响你自己的Python类. 单下划 ...
- python之multiprocessing创建进程
python的multiprocessing模块是用来创建多进程的,下面对multiprocessing总结一下使用记录. multiprocessing创建多进程在windows和linux系统下的 ...
- python并发编程之gevent协程(四)
协程的含义就不再提,在py2和py3的早期版本中,python协程的主流实现方法是使用gevent模块.由于协程对于操作系统是无感知的,所以其切换需要程序员自己去完成. 系列文章 python并发编程 ...
- python 使用multiprocessing需要注意的问题
我们在编写程序的时候经常喜欢这样写代码 import MySQLdb import time from multiprocessing import Process conn = MySQLdb.co ...
随机推荐
- Parameter 0 of method redisTemplate in org.springframework.boot.autoconfigure.data.redis.RedisAutoConfiguration required a bean of type 'org.springframework.data.redis.connection.RedisConnectionFactor
Error starting ApplicationContext. To display the conditions report re-run your application with 'de ...
- mac上安装Nginx详细教程
1. 安装(可以用 brew 安装) sudo brew install nginx 2. 查看 nginx 版本 nginx -v 3. 启动 nginx sudo nginx 1也可以使用下面的命 ...
- django设置时区与语言
django的目录下,django/conf/locale,这个目录下,看有什么语言包, zh_Hans代表中文简体,zh_Hant代表中文繁体,设置即可. TIME_ZONE设置为:Asia/Sha ...
- ADB命令使用详解
ADB是一个 客户端-服务器端 程序, 其中客户端是你用来操作的电脑, 服务器端是android设备. 1.连接android设置 adb connect 设备名 例如: adb connect 12 ...
- [GraphQL] Query Lists of Multiple Types using a Union in GraphQL
Unions are used when we want a GraphQL field or list to handle multiple types of data. With a Union ...
- 十三.基础邮件服务、parted分区工具、交换分区、链路聚合
1.基础邮件服务 DNS服务器:虚拟机classroom 以server0.example.com 为例 yg@server0.example.com xln@server0.exampl ...
- Poj 2599 Godfather(树的重心)
Godfather Time Limit: 2000MS Memory Limit: 65536K Description Last years Chicago was full of gangste ...
- 洛谷P1419寻找段落
题目 单调队列+前缀和 #include <bits/stdc++.h> #define N 101001 using namespace std; int n, s, t; int da ...
- Redis Mysql 双写一致性问题
一:序 - 最近在对数据做缓存时候,会涉及到如何保证 数据库/Redis 一致性问题. - 刚好今天来总结下 一致性问题 产生的问题,和可能存在的解决方案. 二:(更新策略)- 先更新数据库,后更新 ...
- windows使用强大的wget工具
原文链接:https://www.cnblogs.com/hzdx/p/6432161.html wget下载地址:http://www.interlog.com/~tcharron/wgetwin. ...