{"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,"search_vec":"'20':29 '2026':30 '3.3.2':25 'address':40 'cadenc':37 'call':19 'compat':41 'concurr':13,45 'coroutin':46 'current':22 'februari':28 'greenlet':1,2,20 'improv':44 'in-process':10 'librari':32 'lightweight':9 'micro':17,48 'micro-thread':16,47 'packag':6 'perform':43 'process':12 'program':14 'provid':8 'python':5 'releas':26,36 'stabl':35 'thread':18,49 'updat':39 'version':23","created_at":"2026-03-28T08:20:52.060842+00:00","updated_at":"2026-04-16T15:28:21.031453+00:00","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,"quickstart_score":80,"quickstart_tag":"verified","pypi_latest":"3.5.1","cli_name":"","cli_version":null,"type":"library","homepage":"https://greenlet.readthedocs.io","github":"https://github.com/python-greenlet/greenlet","docs":"https://greenlet.readthedocs.io","changelog":"https://greenlet.readthedocs.io/en/latest/changes.html","pypi":"https://pypi.org/project/greenlet/","npm":null,"openapi_spec":null,"status_page":null,"smithery":null,"categories":["serialization"],"base_url":null,"auth_type":null,"provenance":{"verified_status":"passing","verified_at":"2026-06-28","last_verified":"2026-06-28","next_check":"2026-07-28","install_tag":"verified"}}