greenlet

Lightweight in-process coroutines for cooperative concurrency in Python

Library
PyPI
v3.5.6
1,852 stars
Custom / Unknown

Repository Health

Pre-computed score based on development activity, maintenance, community, maturity, and trend momentum. How we score it →
71 /100 Good
Development Activity 68
Maintenance 32
Community 84
Maturity 60
Momentum 40

Technical Analysis

AI-assessed by reading the actual repository — architecture, code quality, innovation, and documentation. How we score it →
71 /100 Good
Architecture 82
Code Quality 78
Innovation 70
Learning Curve 55

greenlet is a C extension that provides lightweight, in-process coroutines for CPython, spun off from Stackless Python’s tasklet model. Instead of relying on the operating system or an event loop to schedule execution, greenlets are explicit micro-threads that you switch between yourself, giving precise control over when control flow moves from one unit of work to another.

Because it exposes no implicit scheduler, greenlet acts as a foundational building block rather than a full concurrency framework: higher-level libraries like gevent and eventlet use it to implement cooperative networking, and SQLAlchemy’s async extension uses it to bridge sync and async code paths.

What You Get

  • A greenlet class representing an independent, switchable execution context with its own call stack
  • Explicit switch() semantics for transferring control and passing values between greenlets
  • Support for nested generator-like control flow without Python’s native yield keyword
  • Tracing hooks (gettrace/settrace) for observing greenlet switches
  • contextvars integration so context state follows greenlet switches correctly
  • Prebuilt wheels covering CPython on Linux, macOS, and Windows across multiple architectures

Common Use Cases

  • Powering gevent’s and eventlet’s cooperative, socket-level concurrency models
  • Bridging synchronous and asynchronous code paths, as SQLAlchemy’s asyncio extension does internally
  • Implementing custom cooperative schedulers or actor-style micro-threads on top of a low-level primitive
  • Building generator-like constructs that can yield from nested function calls

Under The Hood

Architecture: greenlet’s core is a small state machine around per-greenlet C stacks. src/greenlet/greenlet.cpp and the TGreenlet*/TMainGreenlet/TUserGreenlet files implement stack allocation, saving/restoring, and the actual switch() transfer of control via platform-specific assembly in slp_platformselect.h and the platform/ directory; PyGreenlet.cpp wraps this machinery as the Python-visible greenlet type. There is no scheduler anywhere in the codebase — every switch is caller-initiated, which is what lets libraries like gevent build their own event loops on top without fighting an existing one.

Tech Stack: Implemented as a CPython C/C++ extension (about 52% C++, 12% C) with a thin Python layer (src/greenlet/__init__.py) re-exporting the compiled _greenlet module’s symbols. Built via setuptools/pyproject.toml, with per-platform assembly stubs to handle stack switching correctly across x86, x86-64, ARM, and other architectures, plus manylinux/macOS/Windows wheel builds in CI.

Code Quality: The src/greenlet/tests/ directory contains over 5,300 lines of tests, including dedicated fail_*.py scripts that exercise crash and error-recovery paths (e.g. fail_switch_three_greenlets.py, fail_cpp_exception.py), plus test_contextvars.py, test_gc.py, and test_interpreter_shutdown.py covering edge cases like garbage collection interaction and interpreter teardown — reflecting the project’s long history of hardening against subtle stack-corruption bugs.

API Design: The public surface is deliberately minimal — greenlet(fn), .switch(*args), getcurrent(), and a couple of tracing hooks — which keeps the primitive easy to wrap but means correct usage (avoiding orphaned greenlets, understanding exception propagation across switches) requires reading the docs rather than guessing from the API shape alone.

Used by 16 apps in this directory

Python
71%
MIT

Langflow

AI Agents · AI Development

155,973

Build, test, and deploy AI agents and RAG workflows visually with native API and MCP server export.

View details
90
Repo Health
85
Technical
64
Dependency
Built with
Python 71%
TypeScript 21%
Updated yesterday
Python
66%
Other

Morphik

AI Development · Databases · Search

3,717

Morphik is an AI-native ingestion and retrieval engine that lets developers store, search, and reason over visually rich documents — scanned PDFs, manuals, slides, and video — without duct-taping together OCR, an embedding model, and a vector database.

View details
64
Repo Health
71
Technical
67
Dependency
Built with
Python 66%
TypeScript 23%
Updated 4 days ago
Python
52%
Other

Odoo

CRM · ERP · Productivity

55,289

The open source ERP platform that integrates CRM, accounting, inventory, manufacturing, and 60+ business apps into one seamlessly connected suite.

View details
87
Repo Health
82
Technical
64
Dependency
Built with
Python 52%
JavaScript 43%
Updated yesterday
Python
62%
Apache 2.0

Polar

Developer Tools · Ecommerce · Invoicing Finance

10,410

Open source payments infrastructure that turns software into a business — subscriptions, usage-based billing, digital products, and merchant-of-record compliance in one platform.

View details
90
Repo Health
82
Technical
66
Dependency
Built with
Python 62%
TypeScript 31%
Updated 2 days ago
Python
45%
Other

Redash

Analytics · Data Engineering

28,849

Redash lets anyone connect to 35+ SQL and NoSQL data sources, write a query in the browser, and turn the result into a shared dashboard — no separate BI suite required.

View details
91
Repo Health
74
Technical
61
Dependency
Built with
Python 45%
JavaScript 29%
TypeScript 19%
Updated 3 days ago
Python
79%
AGPL 3.0

Skyvern

AI Agents · Automation

23,257

Skyvern (YC S2023) automates browser-based workflows by pairing LLMs with computer vision, letting agents click, fill, and extract data on sites they've never seen, without brittle XPath selectors that break on every layout change.

View details
89
Repo Health
82
Technical
70
Dependency
Built with
Python 79%
TypeScript 19%
Updated yesterday
Python
68%
AGPL 3.0

Tracecat

AI Agents · Automation · Security

3,860

Open-source agentic security automation platform that runs AI agents and durable workflows at scale with sandboxed execution.

View details
89
Repo Health
88
Technical
66
Dependency
Built with
Python 68%
TypeScript 30%
Updated yesterday

Join founders buildingwith open source

Opinionated takes, migration guides, cost-saving tips, and insights from the open source ecosystem.

Subscribe on Substack
Join 750+ subscribers