限制并发greenlets的速率

时间:2012-12-06 05:48:28

标签: python concurrency gevent rate

为了实现greenlet限制,我使用gevent.queue.Queue来实现令牌桶 将模式转换为函数装饰器:

from functools import wraps
from timeit import default_timer
import gevent
from gevent.queue import Queue

def gevent_throttle(calls_per_sec=0):
    """Decorates a Greenlet function for throttling."""
    interval = 1. / calls_per_sec if calls_per_sec else 0
    def decorate(func):
        tq = Queue(1)
        tq.put(0.)
        @wraps(func)
        def throttled_func(*args, **kwargs):
            if calls_per_sec:
                last, current = tq.get(), default_timer()
                elapsed = current - last
                if elapsed < interval:
                    gevent.sleep(interval - elapsed)
                tq.put_nowait(default_timer())
            return func(*args, **kwargs)
        return throttled_func
    return decorate

示例:

import requests
from gevent.pool import Pool

pool = Pool()

@gevent_throttle(10)
def worker(address):
    """Geocodes an address against Google Maps."""
    print address
    url='http://maps.googleapis.com/maps/api/geocode/json'
    params = dict(address=address, sensor='false')
    json = requests.get(url, params=params).json
    return json

jobs = pool.imap(worker, 'NYC LA SF Chicago Boston Austin Portland'.split())

一个明显的缺点是,工人以无序的方式从队列中获取任务。但是,我想,大多数用例在工作者greenlets中不需要任何特定的顺序。

您是否看到其他任何缺点?还有其他选择吗?

0 个答案:

没有答案