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的组合使用的更多相关文章

  1. python3下multiprocessing、threading和gevent性能对比----暨进程池、线程池和协程池性能对比

    python3下multiprocessing.threading和gevent性能对比----暨进程池.线程池和协程池性能对比   标签: python3 / 线程池 / multiprocessi ...

  2. python 协程库gevent学习--gevent数据结构及实战(三)

    gevent学习系列第三章,前面两章分析了大量常用几个函数的源码以及实现原理.这一章重点偏向实战了,按照官方给出的gevent学习指南,我将依次分析官方给出的7个数据结构.以及给出几个相应使用他们的例 ...

  3. python中multiprocessing.pool函数介绍_正在拉磨_新浪博客

    python中multiprocessing.pool函数介绍_正在拉磨_新浪博客     python中multiprocessing.pool函数介绍    (2010-06-10 03:46:5 ...

  4. python的multiprocessing模块进程创建、资源回收-Process,Pool

    python的multiprocessing有两种创建进程的方式,每种创建方式和进程资源的回收都不太相同,下面分别针对Process,Pool及系统自带的fork三种进程分析. 1.方式一:fork( ...

  5. Python下划线简介

    Python中下划线的5种含义 分享一篇文章:The Meaning of Underscores in Python. 本文介绍了Python中单下划线和双下划线("dunder" ...

  6. python下划线的5种含义

    本文介绍了Python中单下划线和双下划线("dunder")的各种含义和命名约定,名称修饰(name mangling)的工作原理,以及它如何影响你自己的Python类. 单下划 ...

  7. python之multiprocessing创建进程

    python的multiprocessing模块是用来创建多进程的,下面对multiprocessing总结一下使用记录. multiprocessing创建多进程在windows和linux系统下的 ...

  8. python并发编程之gevent协程(四)

    协程的含义就不再提,在py2和py3的早期版本中,python协程的主流实现方法是使用gevent模块.由于协程对于操作系统是无感知的,所以其切换需要程序员自己去完成. 系列文章 python并发编程 ...

  9. python 使用multiprocessing需要注意的问题

    我们在编写程序的时候经常喜欢这样写代码 import MySQLdb import time from multiprocessing import Process conn = MySQLdb.co ...

随机推荐

  1. hexo Yelee 主题的busuanzi网站统计没数字显示问题解决

    j进入你的Yelee主题路径: themes\yelee\source\css\_partial 修改该路径下的: footer.styl 修改文件内容: 将内容改为: 然后你的站点就可以了

  2. microsoft office 2007 在已经安装pdf maker的情况下另存为没有adobe pdf选项

    通常,此类情况是pdf maker 插件被禁用导致,点击office 2007左上角菜单栏,选项,加载项,在管理处选择禁用项目, 找到acrobat pdf maker office com addi ...

  3. apache/tomcat笔记

    apache是什么? apache http server 简称apache是世界上排名前列的web服务器,因开源,简单,高性能,速度快,还可以做代理服务器,所以广受人们欢迎 httpd:httpd是 ...

  4. cookie和session基础知识学习

    一.session的简单使用 session是服务器端技术,服务器在运行时可以为每一个用户的浏览器创建一个独享的session对象.session的使用步骤: 获取session对象使用session ...

  5. fsLayuiPlugin入门使用

    简介 源码下载后,不能直接打开,必须运行在容器下,例如:nginx.tomcat.jetty等容器. 源码中默认配置了nginx容器,可以直接启动nginx访问. 本文主要介绍下载源码后的使用,避免在 ...

  6. go语言的内建变量类型

    string bool int int8  int16 int32 int64 uintptr   无符号int 类型  (u)int (u)int8 (u)int16 (u)int32 (u)int ...

  7. Codeforces Round #597 (Div. 2) C. Constanze's Machine

    链接: https://codeforces.com/contest/1245/problem/C 题意: Constanze is the smartest girl in her village ...

  8. Ftp客户端需要TSL功能的文件上传

    Ftp客户端需要TSL功能 1.由于最近做了一个项目,需要把打包的文件传输到对方的FTP服务器上,但是用普通的java连接ftp客户端总是连接不上去,对方却说ftp客户端需要开通TSL功能. 直接上代 ...

  9. myeclipse关掉references

    去掉下面两个勾选:

  10. Greenplum 表空间和filespace的用法

    转载:https://yq.aliyun.com/articles/190 Greenplum支持表空间,创建表空间时,需要指定filespace.postgres=# \h create table ...