Introduction
gevent is a coroutine-based Python networking library that uses greenlet to provide lightweight cooperative concurrency. By monkey-patching the standard library, gevent lets developers write ordinary synchronous code that automatically yields during I/O operations, achieving concurrent performance without callbacks or async/await syntax. It powers high-concurrency workloads in web servers, scrapers, and network services.
What gevent Does
- Runs thousands of lightweight greenlets in a single thread with cooperative scheduling
- Monkey-patches the standard library so socket, ssl, threading, and subprocess become non-blocking
- Provides a fast event loop built on libev (or libuv) for I/O multiplexing
- Includes gevent.pool, gevent.queue, and gevent.event for structured concurrency patterns
- Ships a WSGI server (
gevent.pywsgi) for serving web applications
Architecture Overview
gevent is built on two core components: greenlet, a C extension that provides stackful coroutines, and libev, a high-performance event loop. When a greenlet performs a blocking I/O call (e.g., socket.recv), gevent intercepts it, registers the file descriptor with the event loop, and switches to another greenlet. When the I/O completes, the event loop resumes the original greenlet. This model gives the simplicity of threads with the low overhead of an event loop.
Self-Hosting & Configuration
- Install with
pip install gevent - Call
monkey.patch_all()at the top of your application to make stdlib non-blocking - Use
gevent.spawn()to launch greenlets andgevent.joinall()to wait for them - Deploy web apps with
gevent.pywsgi.WSGIServeror Gunicorn's gevent worker - Tune the
GEVENT_RESOLVERenvironment variable to choose between ares and dnspython resolvers
Key Features
- Write synchronous code that runs concurrently without async/await
- Handle tens of thousands of concurrent connections in a single process
- Compatible with most existing Python libraries through monkey-patching
- Built-in connection pooling, timeouts, and DNS resolution
- Mature project with over a decade of production use
Comparison with Similar Tools
- asyncio — Python standard library async; gevent lets you avoid async/await syntax entirely
- Tornado — async web framework; gevent can serve any WSGI app concurrently
- Twisted — callback-based networking; gevent uses synchronous style
- uvloop — fast asyncio event loop; gevent uses greenlets instead of coroutines
- threading — OS threads with GIL overhead; gevent greenlets are much lighter
FAQ
Q: Does monkey-patching break third-party libraries? A: Most I/O libraries work fine. Libraries with C extensions that bypass the Python socket layer may not be patched automatically.
Q: Can I use gevent with Django?
A: Yes. Run Django under Gunicorn with the gevent worker class: gunicorn -k gevent myapp.wsgi.
Q: How does gevent compare to asyncio for new projects? A: asyncio is the standard and has broader library support. gevent excels when you need to make existing synchronous code concurrent without rewriting it.
Q: What happens if a greenlet blocks on CPU work?
A: CPU-bound work blocks the event loop. Use gevent.threadpool to offload CPU tasks to OS threads.