Question

Is there a way to query number of greenlets in current Gevent process, and the state of them?

For example, I wanna crawle arbitary websites with arbitary greenlets, then I run another greenlet for query how many are running are how many are finished/exception.

Or should I just set a global variable as counter? Does Gevent has something like that built-in?

Was it helpful?

Solution

Gevent doesn't have something like that. But you can use bool(g), g.ready() and g.successful() to check its status. I would poll the status of greenlets this way:

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)
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top