{"id":635,"library":"gevent","title":"Gevent","description":"Gevent is a coroutine-based Python networking library that uses greenlet to provide a high-level synchronous API on top of the libev or libuv event loop. It enables writing concurrent code that looks sequential, primarily for I/O-bound tasks. The current version is 25.9.1, and it follows a CalVer (YY.0M.Micro) release cadence, aiming for at least monthly releases if changes are present in master.","status":"active","version":"25.9.1","language":"python","source_language":"en","source_url":"https://github.com/gevent/gevent","tags":["concurrency","networking","greenlets","async","event loop","monkey patching","I/O bound"],"install":[{"cmd":"pip install gevent","lang":"bash","label":"Install Gevent"}],"dependencies":[{"reason":"Core dependency for lightweight execution units (greenlets).","package":"greenlet","optional":false},{"reason":"Installed by default on Windows, and will become the default on all platforms in a future release for underlying event loop interactions.","package":"cffi","optional":true},{"reason":"Used for cooperative DNS queries, often bundled with gevent.","package":"c-ares","optional":true},{"reason":"One of the underlying event loop implementations.","package":"libev","optional":true},{"reason":"One of the underlying event loop implementations (default).","package":"libuv","optional":true}],"imports":[{"note":"While `import gevent.monkey` works, `from gevent import monkey` is a common idiom for brevity, especially before calling `patch_all()`.","wrong":"import gevent.monkey; gevent.monkey.patch_all()","symbol":"monkey","correct":"from gevent import monkey; monkey.patch_all()"},{"note":"Schedules a function to run as a greenlet.","symbol":"spawn","correct":"import gevent; g = gevent.spawn(my_function, arg1, arg2)"},{"note":"Waits for multiple greenlets to complete.","symbol":"joinall","correct":"import gevent; gevent.joinall([g1, g2])"},{"note":"Cooperatively yields control to other greenlets for a specified duration or immediately if 0.","symbol":"sleep","correct":"import gevent; gevent.sleep(0)"},{"note":"Direct access to the Greenlet class for advanced usage like subclassing.","symbol":"Greenlet","correct":"from gevent import Greenlet"},{"note":"Provides greenlet-local storage, similar to threading.local.","symbol":"local","correct":"from gevent.local import local"}],"quickstart":{"code":"from gevent import monkey; monkey.patch_all()\nimport gevent\nimport requests\n\ndef fetch_url(url):\n    print(f\"Fetching {url}...\")\n    try:\n        response = requests.get(url, timeout=5) # requests uses patched socket\n        print(f\"Finished fetching {url}, status: {response.status_code}\")\n        return len(response.content)\n    except requests.exceptions.RequestException as e:\n        print(f\"Error fetching {url}: {e}\")\n        return 0\n\nurls = [\n    \"https://www.google.com\",\n    \"https://www.github.com\",\n    \"https://www.python.org\"\n]\n\n# Spawn greenlets for each URL fetch\ngreenlets = [gevent.spawn(fetch_url, url) for url in urls]\n\n# Wait for all greenlets to complete\ngevent.joinall(greenlets)\n\ntotal_bytes = sum(g.value for g in greenlets if g.successful())\nprint(f\"Total bytes fetched: {total_bytes}\")","lang":"python","description":"This quickstart demonstrates the core usage of Gevent: first, it applies monkey patching to make standard library functions cooperative. Then, it spawns multiple greenlets to perform I/O-bound tasks (fetching URLs) concurrently, and finally, it waits for all of them to complete."},"warnings":[{"fix":"Place `from gevent import monkey; monkey.patch_all()` at the absolute top of your main script or entry point, even before other imports. If using a framework like Gunicorn with `gevent` workers, check its documentation as it might handle patching for you.","message":"Monkey patching (`gevent.monkey.patch_all()`) must be performed as early as possible in your application's lifecycle, ideally as the very first lines of code in your main module, on the main thread, and before any other modules that might use blocking I/O are imported. Delaying this can lead to unpredictable behavior, conflicts, or errors because parts of the standard library might already be imported in their unpatched, blocking versions.","severity":"gotcha","affected_versions":"All versions"},{"fix":"If you need multiprocessing with Gevent, consider using `gipc` (gevent-cooperative child processes and IPC), which is designed to integrate safely with Gevent. Alternatively, explicitly disable `socket` patching if `multiprocessing` is used.","message":"Using Python's `multiprocessing` module in conjunction with `gevent.monkey.patch_all()` (especially if `socket` is patched, which is the default) can be highly problematic and lead to subtle, hard-to-debug issues, as `multiprocessing` relies on standard (unpatched) OS-level threading/socket behavior.","severity":"gotcha","affected_versions":"All versions"},{"fix":"For CPU-bound tasks, use `multiprocessing` or a task queue system with multiple worker processes. Only apply Gevent where your application's bottleneck is I/O waiting time.","message":"Gevent is designed to make I/O-bound operations (like network requests, disk I/O) concurrent by yielding control during blocking calls. It does not provide true parallelism for CPU-bound tasks within a single process due to Python's Global Interpreter Lock (GIL). Using Gevent for CPU-intensive workloads will not improve performance and can actually make your application slower by adding cooperative multitasking overhead.","severity":"gotcha","affected_versions":"All versions"},{"fix":"Use `gevent.signal_handler` directly when handling signals.","message":"The magic proxy object `gevent.signal`, which served as both a deprecated alias for `gevent.signal_handler` and the `gevent.signal` module, was removed because it caused confusion for users and static analysis tools.","severity":"deprecated","affected_versions":"1.5 and later"},{"fix":"Ensure `gevent.monkey.patch_all()` is called at the absolute earliest point in your application, before any other imports, especially those that might implicitly or explicitly import `concurrent.futures` or related threading modules. You may need to restructure imports if this issue arises.","message":"In Gevent v25, changes in import ordering might break projects relying on `gevent.monkey` if `concurrent.futures` is imported before user-defined code. This is because `concurrent.futures` can hold references to unpatched threading threads that cannot be patched by Gevent later.","severity":"breaking","affected_versions":"25.x.x and later"},{"fix":"Avoid using Gevent with CPython interpreters configured for experimental free-threading mode. Ensure your environment uses a standard CPython build if you rely on Gevent.","message":"Gevent does not support CPython's experimental free-threading mode (introduced in Python 3.13 and 3.14). If used in such an interpreter, Gevent (and its underlying `greenlet` library) will cause the GIL to be re-enabled. Attempting to use Gevent in free-threading mode is not recommended and may lead to resource leaks.","severity":"gotcha","affected_versions":"CPython 3.13, 3.14+ (when free-threading is enabled)"},{"fix":"Ensure a C compiler and other necessary build tools are installed in your environment before attempting to install gevent. For Debian-based systems (like `python:*-slim`), you can install them using `apt-get update && apt-get install -y build-essential`. For Alpine, use `apk add gcc musl-dev`.","message":"Gevent, like many Python packages with C extensions, requires a C compiler (e.g., gcc) to be present in the environment during installation. Minimal Docker images (e.g., `python:X.Y-slim` or `alpine`) often do not include build tools by default, leading to installation failures.","severity":"breaking","affected_versions":"All versions"},{"fix":"Install the 'requests' library using pip: `pip install requests`. Ensure all necessary dependencies are listed in a `requirements.txt` file and installed in the build process.","message":"The Python 'requests' library is not installed in the environment. This will cause a `ModuleNotFoundError` when the application attempts to import it.","severity":"breaking","affected_versions":"All versions"}],"env_vars":null,"last_verified":"2026-05-12T17:02:54.025Z","next_check":"2026-06-26T00:00:00.000Z","problems":[{"fix":"Install `gevent` using pip: `pip install gevent`. Ensure you are in the correct virtual environment if you are using one, and that the Python interpreter running your code is the one for which `gevent` was installed.","cause":"The `gevent` library is not installed in the current Python environment or is not accessible in the `PYTHONPATH`. This can also occur if `gevent` was installed for a different Python version than the one currently in use.","error":"ModuleNotFoundError: No module named 'gevent'"},{"fix":"Identify and replace the blocking operation with its `gevent`-cooperative equivalent (e.g., using `gevent.socket` instead of `socket`), offload CPU-bound tasks to a thread pool (`gevent.threadpool`) or a separate process, or ensure that `gevent.monkey.patch_all()` is called at the very beginning of the application to patch standard library blocking calls.","cause":"This error occurs when a greenlet attempts a synchronous, blocking I/O operation (or a CPU-intensive task) that `gevent` cannot make cooperative, and there are no other active greenlets to switch to, effectively halting the event loop. It can also be caused by greenlets deadlocking on a lock or incorrectly using `gevent` objects with native thread affinity from different threads.","error":"gevent.hub.LoopExit: This operation would block forever"},{"fix":"Install the required build dependencies for your operating system (e.g., `sudo apt-get install build-essential python3-dev libevent-dev` on Debian/Ubuntu, or similar for other distros). Upgrade `pip` and `setuptools` to their latest versions (`pip install --upgrade pip setuptools`). Ensure your Python version is compatible with the `gevent` version you are trying to install.","cause":"This generic error during `gevent` installation, often accompanied by messages about `gcc` or missing headers, indicates a failure to compile `gevent` from source. This typically happens when essential build tools (like `gcc`), Python development headers (`python-devel` or `python3-devel`), or `libevent-dev` are missing from the system. It can also be due to an outdated `setuptools` or `pip` version, or incompatibility with a specific Python or `greenlet` version.","error":"ERROR: Command errored out with exit status 1: command: python setup.py egg_info"},{"fix":"Replace `from gevent.wsgi import WSGIServer` with `from gevent.pywsgi import WSGIServer`. The `gevent.pywsgi` module is the modern replacement for WSGI server functionality.","cause":"The `gevent.wsgi` module has been deprecated and was removed in `gevent` version 1.3.","error":"ModuleNotFoundError: No module named 'gevent.wsgi'"},{"fix":"Ensure `from gevent import monkey; monkey.patch_all()` is executed as early as possible in the lifecycle of *every* process where cooperative concurrency is desired, including subprocesses. For frameworks like Gunicorn, this is often handled automatically, but for custom subprocesses, you might need to explicitly call it within the subprocess's entry point.","cause":"This occurs when `gevent.monkey.patch_all()` is not applied in subprocesses or is applied too late in their execution, meaning standard library functions within those subprocesses remain unpatched and blocking. This is particularly problematic in scenarios like `pytest-xdist` or custom multiprocessing setups.","error":"Monkey patching not being set in subprocesses"}],"ecosystem":"pypi","meta_description":null,"install_score":100,"install_tag":"verified","quickstart_score":0,"quickstart_tag":"stale","pypi_latest":"26.4.0","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":" $EXIT -eq 0 ","exit_code":0,"wheel_type":"sdist","failure_reason":null,"install_time_s":null,"import_time_s":0.22,"mem_mb":7.1,"disk_size":"34.3M"},{"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.26,"mem_mb":7.1,"disk_size":"34.3M"},{"runtime":"python:3.10-slim","python_version":"3.10","os_libc":"slim (glibc)","variant":" $EXIT -eq 0 ","exit_code":0,"wheel_type":"wheel","failure_reason":null,"install_time_s":3,"import_time_s":0.17,"mem_mb":7.1,"disk_size":"32M"},{"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.17,"mem_mb":7.1,"disk_size":"32M"},{"runtime":"python:3.11-alpine","python_version":"3.11","os_libc":"alpine (musl)","variant":" $EXIT -eq 0 ","exit_code":0,"wheel_type":"sdist","failure_reason":null,"install_time_s":null,"import_time_s":0.32,"mem_mb":7.8,"disk_size":"37.7M"},{"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.36,"mem_mb":7.8,"disk_size":"37.7M"},{"runtime":"python:3.11-slim","python_version":"3.11","os_libc":"slim (glibc)","variant":" $EXIT -eq 0 ","exit_code":0,"wheel_type":"wheel","failure_reason":null,"install_time_s":2.5,"import_time_s":0.27,"mem_mb":7.9,"disk_size":"35M"},{"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.26,"mem_mb":7.8,"disk_size":"35M"},{"runtime":"python:3.12-alpine","python_version":"3.12","os_libc":"alpine (musl)","variant":" $EXIT -eq 0 ","exit_code":0,"wheel_type":"sdist","failure_reason":null,"install_time_s":null,"import_time_s":0.28,"mem_mb":8.3,"disk_size":"29.1M"},{"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.28,"mem_mb":8.3,"disk_size":"29.1M"},{"runtime":"python:3.12-slim","python_version":"3.12","os_libc":"slim (glibc)","variant":" $EXIT -eq 0 ","exit_code":0,"wheel_type":"wheel","failure_reason":null,"install_time_s":2.3,"import_time_s":0.27,"mem_mb":8.3,"disk_size":"26M"},{"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.27,"mem_mb":8.3,"disk_size":"26M"},{"runtime":"python:3.13-alpine","python_version":"3.13","os_libc":"alpine (musl)","variant":" $EXIT -eq 0 ","exit_code":0,"wheel_type":"sdist","failure_reason":null,"install_time_s":null,"import_time_s":0.23,"mem_mb":7.7,"disk_size":"28.9M"},{"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.24,"mem_mb":7.7,"disk_size":"28.7M"},{"runtime":"python:3.13-slim","python_version":"3.13","os_libc":"slim (glibc)","variant":" $EXIT -eq 0 ","exit_code":0,"wheel_type":"wheel","failure_reason":null,"install_time_s":2.4,"import_time_s":0.24,"mem_mb":7.7,"disk_size":"26M"},{"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.24,"mem_mb":7.7,"disk_size":"26M"},{"runtime":"python:3.9-alpine","python_version":"3.9","os_libc":"alpine (musl)","variant":" $EXIT -eq 0 ","exit_code":1,"wheel_type":null,"failure_reason":"build_error","install_time_s":null,"import_time_s":null,"mem_mb":null,"disk_size":null},{"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.28,"mem_mb":5.9,"disk_size":"33.1M"},{"runtime":"python:3.9-slim","python_version":"3.9","os_libc":"slim (glibc)","variant":" $EXIT -eq 0 ","exit_code":1,"wheel_type":null,"failure_reason":"build_error","install_time_s":11.3,"import_time_s":null,"mem_mb":null,"disk_size":null},{"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.23,"mem_mb":5.9,"disk_size":"31M"}]},"quickstart_checks":{"last_tested":"2026-04-24","tag":"stale","tag_description":"widespread failures or data too old to trust","results":[{"runtime":"python:3.10-alpine","exit_code":1},{"runtime":"python:3.10-slim","exit_code":1},{"runtime":"python:3.11-alpine","exit_code":1},{"runtime":"python:3.11-slim","exit_code":1},{"runtime":"python:3.12-alpine","exit_code":1},{"runtime":"python:3.12-slim","exit_code":1},{"runtime":"python:3.13-alpine","exit_code":1},{"runtime":"python:3.13-slim","exit_code":1},{"runtime":"python:3.9-alpine","exit_code":0},{"runtime":"python:3.9-slim","exit_code":0}]}}