Skip to main content

gtools

A python library providing gevent tools:

  • a gevent friendly pdb
  • tree representation of running greenlets

Gevent friendly pdb

The standard python pdb module blocks all greenlets at the pdb prompt (unlike a threaded app). If you want your greenlets to run in the background you can use gtools.pdb instead.

It can be used like the standard pdb.

So, imagine you have a gevent app that you want to debug:

# ======
# app.py
# ======

import gevent

def produce(p):
    for i in range(60):
        p.append(i)
        gevent.sleep(1)

products = []
gevent.spawn(produce, products)

to debug it just type on the console:

$ python -m gtools.pdb app.py

Then hit 'n' until you reach the task.join() line. At this point the greenlet is already doing its work on the background. To make sure just type products several times on the pdb console and you will see the products list being filled by the running greenlet:

> /app.py(9)<module>()
-> gevent.spawn(produce, products)
(Pdb) products
[0, 1, 2]
(Pdb) products
[0, 1, 2, 3, 4, 5, 6, 7]

Use gtools.pdb.set_trace() just as you would with the standard pdb.set_trace()

Monitoring greenlets

gtools.tree.Tree() allows you to trace the current greenlets and display them in a tree like structure:

>>> import gevent
>>> import gtools.tree

>>> def iloop():
...     gevent.sleep(1)

>>> def oloop():
...     gtools.spawn(iloop)
...     gevent.sleep(0.5)

>>> task = gtools.spawn(oloop)

>>> # sleep just to trigger spawn of inner greenlets
>>> gevent.sleep()
>>> tree = gtools.tree.Tree()

>>> # initial status
>>> print(tree)
Root
└─ <greenlet.greenlet A> status=running
    └─ <Greenlet B: oloop> status=running
        └─ <Greenlet C: iloop> status=running

>>> # after outer loop finishes
>>> gevent.sleep(0.6)
>>> print(tree)
Root
└─ <greenlet.greenlet A> status=running
    └─ <Greenlet B: oloop> status=finished:success
        └─ <Greenlet C: iloop> status=running

>>> # after inner loop finishes
>>> gevent.sleep(0.6)
>>> print(tree)
Root
└─ <greenlet.greenlet A> status=running
    └─ <Greenlet B: oloop> status=finished:success
        └─ <Greenlet C: iloop> status=dead:garbage collected

>>> del task

>>> # when there are no more references to the greenlets
>>> print(tree)
Root
└─ <greenlet.greenlet A> status=running
    └─ <Greenlet B: oloop> status=dead:garbage collected
        └─ <Greenlet C: iloop> status=dead:garbage collected

>>>
>>> # new tree
>>> print(gtools.tree.Tree())
Root

The above example requires the usage of gtools.Greenlet.

To trace greenlets from an existing gevent application you simply need to monkey-patch gevent itself before importing your app:

# ======
# app.py
# ======

import gevent

def iloop():
    gevent.sleep(1)

def oloop():
    gevent.spawn(iloop)
    gevent.sleep(0.5)

def run():
    return gevent.spawn(oloop)
>>> from gtools.monkey import patch_gevent
>>> patch_gevent()
>>> import app
>>> import gtools.tree

>>> the_app = app.run()

>>> # sleep just to trigger spawn of inner greenlets
>>> gevent.sleep()
>>> tree = gtools.tree.Tree()

>>> # initial status
>>> print(tree)
Root
└─ <greenlet.greenlet A> status=running
    └─ <Greenlet B: oloop> status=running
        └─ <Greenlet C: iloop> status=running

If you don't monkey patch, you can still have limited information about the running greenlets (notice that the tree hierarchy is lost):

>>> import app
>>> import gtools.tree
>>> task = gevent.spawn(oloop)

>>> the_app = app.run()

>>> # sleep just to trigger spawn of inner greenlets
>>> gevent.sleep()
>>> tree = gtools.tree.Tree(all=True)

>>> # initial status
>>> print(tree)
Root
└─ <greenlet.greenlet A> status=running
    └─ <Hub [...]> status=running
        ├─ <Greenlet B: oloop> status=running
        └─ <Greenlet C: iloop> status=running

It can even trace greenlets across multiple threads (see examples/tree_threads.py)

Metadata

Release files for gevent-tools 0.3.1

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for gevent-tools 0.3.1
File Size Uploaded
gevent-tools-0.3.1.tar.gz 7.7 kB Details

Release files / gevent-tools-0.3.1.tar.gz

Download URL gevent-tools-0.3.1.tar.gz
Size 7.7 kB
Tags Source
SHA-256 checksum
How to use checksums
162a6af50931aa07b27da6c831ce362d5668d7a7f8426ccd25255d23fd4638ad
BLAKE2b-256 checksum
How to use checksums
746d7b281a381d65fa7e73d50e1cec3dbde744f3d477cc436b762600277a203e
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/0.0.0 importlib_metadata/4.6.1 pkginfo/1.7.1 requests/2.26.0 requests-toolbelt/0.9.1 tqdm/4.62.0 CPython/3.9.6

Release history Release notifications | RSS feed

This release

0.3.1 This release

1 release file

0.2.0

1 release file

0.1.1

1 release file

0.1.0

1 release file

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page