Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get total number of running greenlets in Gevent?

Tags:

python

gevent

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?

like image 352
est Avatar asked Jul 03 '13 06:07

est


People also ask

What is gevent greenlet?

gevent is a coroutine -based Python networking library that uses greenlet to provide a high-level synchronous API on top of the libev or libuv event loop. Features include: Fast event loop based on libev or libuv. Lightweight execution units based on greenlets.

Is gevent multithreaded?

¶ Gevent is the use of simple, sequential programming in python to achieve scalability provided by asynchronous IO and lightweight multi-threading (as opposed to the callback-style of programming using Twisted's Deferred).

What does gevent spawn do?

New greenlets are spawned by creating a Greenlet instance and calling its start method. (The gevent. spawn() function is a shortcut that does exactly that). The start method schedules a switch to the greenlet that will happen as soon as the current greenlet gives up control.

What is gevent Eventlet?

gevent is a coroutine-based cooperative multitasking python framework that relies on monkey patching to make all code cooperative. Gevent actually draws its lineage from Eve Online which was implemented using Stackless Python which eventually evolved into eventlet which inspired gevent.


1 Answers

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)
like image 198
suzanshakya Avatar answered Oct 31 '22 13:10

suzanshakya