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?
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.
¶ 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).
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.
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.
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)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With