获取Gevent中运行greenlet的总数?

时间:2013-07-03 06:52:35

标签: python gevent

有没有办法查询当前Gevent流程中的greenlet数量及其状态?

例如,我想用仲裁greenlets抓取任意网站,然后我运行另一个greenlet来查询有多少正在运行的是完成/异常的数量。

或者我应该将全局变量设置为计数器? Gevent是否有内置的东西?

1 个答案:

答案 0 :(得分:7)

Gevent没有类似的东西。但您可以使用bool(g)g.ready()g.successful()来检查其状态。我会用这种方式评估greenlets的状态:

import gevent
import random

def _get_status(greenlets):
    total = 0
    running = 0
    completed = 0
    successed = 0
    yet_to_run = 0
    failed = 0

    for g in greenlets:
        total += 1
        if bool(g):
            running += 1
        else:
            if g.ready():
                completed += 1
                if g.successful():
                    successed += 1
                else:
                    failed += 1
            else:
                yet_to_run += 1

    assert yet_to_run == total - completed - running
    assert failed == completed - successed

    return dict(total=total,
                running=running,
                completed=completed,
                successed=successed,
                yet_to_run=yet_to_run,
                failed=failed)

def get_greenlet_status(greenlets):
    while True:
        status = _get_status(greenlets)
        print status
        if status['total'] == status['completed']:
            return
        gevent.sleep(5)

def crawl(url):
    r = random.randint(0, 10)
    gevent.sleep(r)

    err = random.randint(0, 4)
    if err == 0:
        raise Exception

greenlets = [gevent.spawn(crawl, each) for each in xrange(100)]

get_greenlet_status(greenlets)