Esta página se muestra en inglés. Una traducción al español está en curso.
ScriptsMay 23, 2026·3 min de lectura

gevent — Coroutine-Based Concurrency for Python

Python networking library that uses greenlets for lightweight concurrent I/O, providing a synchronous coding style with asynchronous performance.

Listo para agents

Este activo puede ser leído e instalado directamente por agents

TokRepo expone un comando CLI universal, contrato de instalación, metadata JSON, plan según adaptador y contenido raw para que los agents evalúen compatibilidad, riesgo y próximos pasos.

Needs Confirmation · 64/100Política: confirmar
Superficie agent
Cualquier agent MCP/CLI
Tipo
Skill
Instalación
Single
Confianza
Confianza: Established
Entrada
gevent Overview
Comando CLI universal
npx tokrepo install 7d985e1e-56e6-11f1-9bc6-00163e2b0d79

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 and gevent.joinall() to wait for them
  • Deploy web apps with gevent.pywsgi.WSGIServer or Gunicorn's gevent worker
  • Tune the GEVENT_RESOLVER environment 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.

Sources

Discusión

Inicia sesión para unirte a la discusión.
Aún no hay comentarios. Sé el primero en compartir tus ideas.

Activos relacionados