python eventlet模块

原创 2015年11月19日 16:17:53

python eventlet模块

python中更实用eventlet模块创建线程或者线程池。

def _func_on_containers(logger, conf, concurrency_key, func, **kwargs):
    """Run a function on each container with concurrency."""

    bench = Bench(logger, conf, [])
    pool = eventlet.GreenPool(int(getattr(conf, concurrency_key)))
    for container in conf.containers:
        pool.spawn_n(func, bench.url, bench.token, container, **kwargs)
    pool.waitall()
eventlet模块内函数
def spawn_n(self, function, *args, **kwargs):
        """Create a greenthread to run the *function*, the same as
        :meth:`spawn`.  The difference is that :meth:`spawn_n` returns
        None; the results of *function* are not retrievable.
        """
        # if reentering an empty pool, don't try to wait on a coroutine freeing
        # itself -- instead, just execute in the current coroutine
        current = greenthread.getcurrent()
        if self.sem.locked() and current in self.coroutines_running:
            self._spawn_n_impl(function, args, kwargs, None)
        else:
            self.sem.acquire()
            g = greenthread.spawn_n(
                self._spawn_n_impl,
                function, args, kwargs, True)
            if not self.coroutines_running:
                self.no_coros_running = event.Event()
            self.coroutines_running.add(g)

def waitall(self):
        """Waits until all greenthreads in the pool are finished working."""
        assert greenthread.getcurrent() not in self.coroutines_running, \
            "Calling waitall() from within one of the " \
            "GreenPool's greenthreads will never terminate."
        if self.running():
            self.no_coros_running.wait()

def spawn(func, *args, **kwargs):
    """Create a greenthread to run ``func(*args, **kwargs)``.  Returns a
    :class:`GreenThread` object which you can use to get the results of the
    call.

    Execution control returns immediately to the caller; the created greenthread
    is merely scheduled to be run at the next available opportunity.
    Use :func:`spawn_after` to  arrange for greenthreads to be spawned
    after a finite delay.
    """
    hub = hubs.get_hub()
    g = GreenThread(hub.greenlet)
    hub.schedule_call_global(0, g.switch, func, args, kwargs)
    return g


def spawn_n(func, *args, **kwargs):
    """Same as :func:`spawn`, but returns a ``greenlet`` object from
    which it is not possible to retrieve either a return value or
    whether it raised any exceptions.  This is faster than
    :func:`spawn`; it is fastest if there are no keyword arguments.

    If an exception is raised in the function, spawn_n prints a stack
    trace; the print can be disabled by calling
    :func:`eventlet.debug.hub_exceptions` with False.
    """
    return _spawn_n(0, func, args, kwargs)[1]

python eventlet

Eventlet(http://eventlet.net/)是一个python的网络库,他可以通过协程的方式来实现并发。Eventlet协程又称GreenThread(绿色线程),所谓的并发,就是创建...

python eventlet 模块学习tpool和greenpool

当我们需要使用到python的c接口,特别是一些对os的系统调用,官方说明如下: The vast majority of the times you’ll want to use threads...

openstack 模块eventlet example code

终于有时间可以仔细研究下openstack依赖的第三方module,主要还是以官方的example入手,熟悉example的思路。 example 01 : 01webcrawler.py 比...

python eventlet并发原理分析

最近在学习eventlet这个强悍的东东,看到我同事的一些整理。故贴出来,大家一起分享~ motivation 114.113.199.11服务器上nova服务中基于python even...

eventlet引发的学习:python:单线程、多线程、多进程在计算方面的性能对比

参考: Python GIL 系列之通过实例认识Python的GIL Python GIL 系列之再谈Python的GIL概述本文通过对比测试来证明下单线程、多线程、多进程的‘并发‘计算能力测试测试...

python基于协程的网络库gevent、eventlet

python网络库也有了基于协程的实现,比较著名的是 gevent、eventlet 它两之间的关系可以参照 Comparing gevent to eventlet, 本文主要简单介绍一下event...

Python并发编程eventlet

1 多进程、多线程和协程 python并发编程之多进程、多线程、异步和协程:http://www.cnblogs.com/tyomcat/p/5486827.html 1.1 总结 1)多进...

协程才是未来-性能夸张的协程服务器,基于eventlet(greenlet)的http性能测试

协程才是未来-性能夸张的协程服务器,基于eventlet(greenlet)的http性能测试 http://gashero.iteye.com/blog/442177 博客分类:  ...

openstack基础_eventlet

greenlet源码流程解析,eventlet使用

eventlet

转载自:http://blog.csdn.net/xiangmin2587/article/details/8182775   1. 首先说一下什么是协程 协同程序与线程差不多,也就是一条执行序...
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:python eventlet模块
举报原因:
原因补充:

(最多只允许输入30个字)