Cette page est affichée en anglais. Une traduction française est en cours.
ScriptsMay 23, 2026·3 min de lecture

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.

Prêt pour agents

Cet actif peut être lu et installé directement par les agents

TokRepo expose une commande CLI universelle, un contrat d'installation, le metadata JSON, un plan selon l'adaptateur et le contenu raw pour aider les agents à juger l'adaptation, le risque et les prochaines actions.

Needs Confirmation · 64/100Policy : confirmer
Surface agent
Tout agent MCP/CLI
Type
Skill
Installation
Single
Confiance
Confiance : Established
Point d'entrée
gevent Overview
Commande CLI universelle
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

Fil de discussion

Connectez-vous pour rejoindre la discussion.
Aucun commentaire pour l'instant. Soyez le premier à partager votre avis.

Actifs similaires