Gevent
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.
Warnings
- gotcha 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.
- gotcha 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.
- gotcha 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.
- deprecated 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.
- breaking 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.
- gotcha 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.
Install
-
pip install gevent
Imports
- monkey
from gevent import monkey; monkey.patch_all()
- spawn
import gevent; g = gevent.spawn(my_function, arg1, arg2)
- joinall
import gevent; gevent.joinall([g1, g2])
- sleep
import gevent; gevent.sleep(0)
- Greenlet
from gevent import Greenlet
- local
from gevent.local import local
Quickstart
from gevent import monkey; monkey.patch_all()
import gevent
import requests
def fetch_url(url):
print(f"Fetching {url}...")
try:
response = requests.get(url, timeout=5) # requests uses patched socket
print(f"Finished fetching {url}, status: {response.status_code}")
return len(response.content)
except requests.exceptions.RequestException as e:
print(f"Error fetching {url}: {e}")
return 0
urls = [
"https://www.google.com",
"https://www.github.com",
"https://www.python.org"
]
# Spawn greenlets for each URL fetch
greenlets = [gevent.spawn(fetch_url, url) for url in urls]
# Wait for all greenlets to complete
gevent.joinall(greenlets)
total_bytes = sum(g.value for g in greenlets if g.successful())
print(f"Total bytes fetched: {total_bytes}")