{"id":297,"library":"greenlet","title":"greenlet","description":"greenlet is a Python package that provides lightweight in-process concurrent programming through micro-threads called 'greenlets'. The current version is 3.3.2, released on February 20, 2026. The library has a stable release cadence, with updates addressing compatibility and performance improvements.","status":"active","version":"3.3.2","language":"python","source_language":"en","source_url":"https://github.com/python-greenlet/greenlet","tags":["concurrency","coroutines","micro-threads"],"install":[{"cmd":"pip install greenlet","lang":"bash","label":"Install greenlet"}],"dependencies":[],"imports":[{"note":"Directly importing 'greenlet' does not provide access to the 'greenlet' class; use 'from greenlet import greenlet' instead.","wrong":"import greenlet","symbol":"greenlet","correct":"from greenlet import greenlet"},{"note":"To access the 'getcurrent' function, use 'from greenlet import getcurrent'.","wrong":"import greenlet","symbol":"getcurrent","correct":"from greenlet import getcurrent"}],"quickstart":{"code":"from greenlet import greenlet\n\ndef test1():\n    print(\"[gr1] main  -> test1\")\n    gr2.switch()\n    print(\"[gr1] test1 <- test2\")\n    return 'test1 done'\n\ndef test2():\n    print(\"[gr2] test1 -> test2\")\n    gr1.switch()\n\ngr1 = greenlet(test1)\ngr2 = greenlet(test2)\ngr1.switch()","lang":"python","description":"This example demonstrates creating and switching between two greenlets. 'gr1' starts 'test1', which switches to 'gr2' running 'test2', and then switches back to 'gr1'."},"warnings":[{"fix":"Upgrade your Python environment to version 3.10 or later.","message":"Support for Python 3.9 was dropped in version 3.3.0.","severity":"breaking","affected_versions":">=3.3.0"},{"fix":"Upgrade your Python environment to version 3.9 or later.","message":"Support for Python 3.7 and 3.8 was removed in version 3.2.0.","severity":"breaking","affected_versions":">=3.2.0"},{"fix":"Ensure that signal handlers return control to the main greenlet without switching.","message":"Mixing greenlets and signal handlers can lead to hangs if the signal handler switches greenlets without returning.","severity":"gotcha","affected_versions":"All versions"},{"fix":"Avoid calling non-reentrant native functions from multiple greenlets.","message":"Using non-reentrant native functions within greenlets can cause subtle issues.","severity":"gotcha","affected_versions":"All versions"},{"fix":"Ensure that greenlet switching occurs within the same thread.","message":"Greenlets cannot switch between different Python threads.","severity":"gotcha","affected_versions":"All versions"},{"fix":"Implement appropriate exception handling to manage 'GreenletExit' during garbage collection.","message":"Garbage collection of greenlets can lead to 'GreenletExit' exceptions if not handled properly.","severity":"gotcha","affected_versions":"All versions"}],"env_vars":null,"last_verified":"2026-05-12T12:55:13.241Z","next_check":"2026-06-26T00:00:00.000Z","problems":[{"fix":"Ensure that all greenlet operations, especially calls to `greenlet.switch()`, occur within the same operating system thread where the greenlet was originally created. If using frameworks like Gevent or Eventlet, ensure that `monkey.patch_all()` is called at the very beginning of your application's execution, before any other threads are spawned or blocking I/O operations are initiated, to properly 'greenify' the standard library. Avoid passing active greenlet instances directly between Python threads.","cause":"This error occurs when an attempt is made to switch execution between greenlets that belong to different operating system threads, which is a fundamental limitation of the greenlet design. Each OS thread maintains its own 'main' greenlet, and greenlets created within one thread cannot be directly switched into from another thread.","error":"greenlet.error: cannot switch to a different thread"},{"fix":"First, try reinstalling `greenlet` with `pip install greenlet --no-cache-dir` to ensure a fresh download and installation. Ensure your `pip` and `setuptools` are up-to-date (`python -m pip install --upgrade pip setuptools`). If you are on Windows, ensure the appropriate Visual C++ Redistributable packages are installed for your Python version. If issues persist, consider installing `greenlet` by compiling from source using `pip install greenlet --no-binary :all:` after confirming that you have the necessary build tools (like a C compiler) installed on your system.","cause":"This error indicates that the underlying C extension module `_greenlet`, which is a critical component of the `greenlet` library, cannot be found or loaded by Python. This typically points to an incomplete or corrupted installation, an incompatible Python version, or missing C runtime libraries (especially on Windows) that prevent the `.pyd` or `.so` file from being loaded.","error":"ModuleNotFoundError: No module named 'greenlet._greenlet'"},{"fix":"Install the required build tools for your operating system: for Debian/Ubuntu, `sudo apt-get install build-essential python3-dev`; for macOS, install Xcode Command Line Tools (`xcode-select --install`); for Windows, install Visual C++ Build Tools from Microsoft. Ensure `pip` and `setuptools` are updated (`python -m pip install --upgrade pip setuptools`). If a specific (potentially older) version of `greenlet` is being requested by another package and causes this error, try to update the dependent package or explicitly install a compatible `greenlet` version.","cause":"This installation error occurs when `pip` attempts to compile the `greenlet` C extension from source, but the compilation process fails. This is commonly caused by the absence of a C compiler (e.g., GCC on Linux, Xcode Command Line Tools on macOS, or Visual C++ Build Tools on Windows), missing Python development headers, or an incompatibility between the Python version, `setuptools`, and the `greenlet` source code.","error":"ERROR: Failed building wheel for greenlet"}],"ecosystem":"pypi","meta_description":null,"install_score":80,"install_tag":"verified","quickstart_score":80,"quickstart_tag":"verified","pypi_latest":null,"install_checks":{"last_tested":"2026-05-12","tag":"verified","tag_description":"installs cleanly on critical runtimes, fast import, recently tested","results":[{"runtime":"python:3.10-alpine","python_version":"3.10","os_libc":"alpine (musl)","variant":"default","exit_code":0,"wheel_type":null,"failure_reason":null,"install_time_s":null,"import_time_s":0,"mem_mb":0,"disk_size":"23.5M"},{"runtime":"python:3.10-slim","python_version":"3.10","os_libc":"slim (glibc)","variant":"default","exit_code":0,"wheel_type":null,"failure_reason":null,"install_time_s":null,"import_time_s":0,"mem_mb":0,"disk_size":"21M"},{"runtime":"python:3.11-alpine","python_version":"3.11","os_libc":"alpine (musl)","variant":"default","exit_code":0,"wheel_type":null,"failure_reason":null,"install_time_s":null,"import_time_s":0,"mem_mb":0,"disk_size":"25.4M"},{"runtime":"python:3.11-slim","python_version":"3.11","os_libc":"slim (glibc)","variant":"default","exit_code":0,"wheel_type":null,"failure_reason":null,"install_time_s":null,"import_time_s":0,"mem_mb":0,"disk_size":"23M"},{"runtime":"python:3.12-alpine","python_version":"3.12","os_libc":"alpine (musl)","variant":"default","exit_code":0,"wheel_type":null,"failure_reason":null,"install_time_s":null,"import_time_s":0,"mem_mb":0,"disk_size":"17.2M"},{"runtime":"python:3.12-slim","python_version":"3.12","os_libc":"slim (glibc)","variant":"default","exit_code":0,"wheel_type":null,"failure_reason":null,"install_time_s":null,"import_time_s":0,"mem_mb":0,"disk_size":"14M"},{"runtime":"python:3.13-alpine","python_version":"3.13","os_libc":"alpine (musl)","variant":"default","exit_code":0,"wheel_type":null,"failure_reason":null,"install_time_s":null,"import_time_s":0,"mem_mb":0,"disk_size":"16.9M"},{"runtime":"python:3.13-slim","python_version":"3.13","os_libc":"slim (glibc)","variant":"default","exit_code":0,"wheel_type":null,"failure_reason":null,"install_time_s":null,"import_time_s":0,"mem_mb":0,"disk_size":"14M"},{"runtime":"python:3.9-alpine","python_version":"3.9","os_libc":"alpine (musl)","variant":"default","exit_code":0,"wheel_type":null,"failure_reason":null,"install_time_s":null,"import_time_s":0,"mem_mb":0,"disk_size":"22.1M"},{"runtime":"python:3.9-slim","python_version":"3.9","os_libc":"slim (glibc)","variant":"default","exit_code":0,"wheel_type":null,"failure_reason":null,"install_time_s":null,"import_time_s":0,"mem_mb":0,"disk_size":"20M"}]},"quickstart_checks":{"last_tested":"2026-04-23","tag":"verified","tag_description":"quickstart runs on critical runtimes, recently tested","results":[{"runtime":"python:3.10-alpine","exit_code":0},{"runtime":"python:3.10-slim","exit_code":0},{"runtime":"python:3.11-alpine","exit_code":0},{"runtime":"python:3.11-slim","exit_code":0},{"runtime":"python:3.12-alpine","exit_code":0},{"runtime":"python:3.12-slim","exit_code":0},{"runtime":"python:3.13-alpine","exit_code":0},{"runtime":"python:3.13-slim","exit_code":0},{"runtime":"python:3.9-alpine","exit_code":0},{"runtime":"python:3.9-slim","exit_code":0}]}}