gevent: tool for high performance Python networking

gevent is a coroutine-based high performance networking library for Python with a high-level synchronous API provided by greenlet and fast event loop based on libev.

gevent is a coroutine-based concurrency library for Python built on top of libev and greenlet. This non-blocking Python network library was inspired by eventlet but with simpler implementation and better performance due to a high-level synchronous API provided by greenlet and fast event loop based on libev. gevent is an open source project written and maintained by Denis Bilenko and is spread under the MIT license.

Greenlet is a lightweight coroutine provided to Python as a C extension module. It is a primary pattern used in gevent that runs inside of the OS process for the main program. Greenlets use cooperative multitasking, though only one greenlet is ever running at any given time. gevent has no built in support for multiprocessing.

Main gevent features include:

  • Synchronization primitives and execution units based on greenlet
  • API that re-uses concepts from the Python standard library (e.g. Event, Queue)
  • Cooperative sockets and ssl modules
  • Monkey patching utility to get third party modules written for standard blocking sockets
  • DNS queries performed through threadpool (by default) or through c-ares
  • Subprocess support (via gevent.subprocess)
  • Basic servers (e.g. TCP/UDP/HTTP) for handling socket-based connections
  • Lightweight pseudothreads
  • Greenlet-local objects and pools

The main advantage of gevent is its high performance, especially in comparison with traditional threading solutions. When gevent is used for network and I/O bound functions which are scheduled cooperatively, it shows its true power. Consistent API looks similar to threaded programming, but underneath transparently runs asynchronous I/O.

All in all, gevent is a great solution for high performance Python networking. It is very fast, with a good ecosystem, that uses greenlets, libev, monkey-patching, and simple async programming model to their full potential. Go to gevent.org for more information.

Connect with our experts Let's talk