Generated by GPT-5-mini| gevent | |
|---|---|
| Name | gevent |
| Author | Greenlet Team |
| Developer | Paul Moore |
| Initial release | 2009 |
| Latest release | 21.12.0 |
| Programming language | Python, C |
| License | MIT License |
| Website | gevent.org |
gevent
gevent is a Python networking library that provides a coroutine-based concurrency model built on greenlet and libev/libuv event loops. It aims to simplify asynchronous I/O for web servers, network services, and concurrent applications while interoperating with many ecosystems like Django, Flask, Twisted, SQLAlchemy, and Celery. gevent is widely used alongside projects such as Gunicorn, uWSGI, and nginx reverse-proxy setups in production environments at organizations including Dropbox, Instagram, and Mozilla.
gevent implements cooperative multitasking using lightweight microthreads called greenlets, which are implemented by the greenlet C extension originally created by Ned Deily and others. The library integrates with event loops like libev and libuv, exposing primitives for asynchronous sockets, DNS resolution, and timeouts. It is often compared to other concurrency frameworks and runtimes such as asyncio, Twisted, Node.js, and Stackless Python for event-driven or coroutine-based designs. gevent's model is compatible with standard Python networking APIs, enabling migration paths from synchronous codebases developed with frameworks like Werkzeug or libraries such as requests.
The core architecture of gevent centers on the scheduler that switches between greenlets, the event loop backed by libev or libuv, and C extensions (greenlet, c-ares) to accelerate context switching and DNS lookups. Key features include non-blocking socket wrappers, cooperative locking primitives, pooled worker patterns used by Gunicorn, and monkey-patching utilities to replace blocking calls in urllib3, httplib, and socket modules. gevent's architecture supports patterns common in event-driven programming such as reactors, proactor-like callbacks found in Twisted, and integration points for WSGI servers like uWSGI and Gunicorn.
Typical usage involves spawning greenlets for concurrent tasks, using gevent's spawn, join, and pool APIs, and performing asynchronous network operations via gevent.socket and gevent.ssl. The API exposes constructs for timeouts, event objects, and threadpool execution that interoperate with threading APIs and synchronization primitives used by projects like Celery or databases accessed through SQLAlchemy. Popular deployment patterns place gevent workers behind reverse proxies like nginx or load balancers such as HAProxy and integrate with process managers like systemd or supervisord.
Benchmarks typically compare gevent to asyncio, Twisted, Node.js, and threaded servers under workloads similar to those used by Netty or Tornado benchmarks. gevent often shows high throughput and low latency for I/O-bound workloads due to minimal context-switch overhead from greenlets and efficient event loop implementations via libev or libuv. Real-world performance reports from companies like Instagram and Dropbox emphasize gevent's low memory footprint and efficient socket handling compared with multi-threaded approaches used by Apache HTTP Server and NGINX Unit in similar scenarios.
gevent provides monkey-patching to adapt standard libraries and integrates with application frameworks such as Django, Flask, Pyramid, and WSGI servers like Gunicorn, uWSGI, and Waitress. It interoperates with ORMs and database drivers such as SQLAlchemy and community drivers for PostgreSQL, MySQL, and SQLite when combined with connection pooling strategies used by pgbouncer or ProxySQL. Integration with testing frameworks like pytest and continuous integration systems like Jenkins or GitHub Actions is common in deployments.
gevent evolved from greenlet-based experiments in the late 2000s and was shaped by contributors from the open-source community including Paul Moore and other maintainers. The project matured alongside event libraries such as libev and libuv and has paralleled developments in Python's own concurrency landscape, including the introduction of PEP 3156 and the asyncio module. Over time, gevent added support for newer versions of Python, adopted improvements from ecosystems like PyPy, and responded to deployment trends exemplified by platforms like Heroku and AWS Elastic Beanstalk.
Security and criticism of gevent focus on monkey-patching risks when replacing standard library behavior, subtle bugs in cooperative multitasking compared to preemptive OS thread scheduling, and compatibility challenges with C extensions that assume blocking semantics (e.g., some drivers for OpenSSL, libpq). Critics point to integration complexity in mixed-threaded environments and caution when using gevent with third-party libraries not designed for cooperative contexts. The project addresses many issues through documentation, tests, and community patches contributed via platforms like GitHub and discussions in forums such as Stack Overflow.
Category:Python (programming language) libraries