greenlet
Lightweight in-process coroutines for cooperative concurrency in Python
Repository Health
Technical Analysis
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
greenletclass 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
yieldkeyword - 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
Langflow
AI Agents · AI Development
Build, test, and deploy AI agents and RAG workflows visually with native API and MCP server export.
Morphik
AI Development · Databases · Search
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.
Odoo
CRM · ERP · Productivity
The open source ERP platform that integrates CRM, accounting, inventory, manufacturing, and 60+ business apps into one seamlessly connected suite.
Polar
Developer Tools · Ecommerce · Invoicing Finance
Open source payments infrastructure that turns software into a business — subscriptions, usage-based billing, digital products, and merchant-of-record compliance in one platform.
Redash
Analytics · Data Engineering
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.
Skyvern
AI Agents · Automation
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.
Tracecat
AI Agents · Automation · Security
Open-source agentic security automation platform that runs AI agents and durable workflows at scale with sandboxed execution.