Scripts2026年5月23日·1 分钟阅读

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.

Agent 就绪

这个资产可以被 Agent 直接读取和安装

TokRepo 同时提供通用 CLI 命令、安装契约、metadata JSON、按适配器生成的安装计划和原始内容链接,方便 Agent 判断适配度、风险和下一步动作。

Needs Confirmation · 64/100策略:需确认
Agent 入口
任意 MCP/CLI Agent
类型
Skill
安装
Single
信任
信任等级:Established
入口
gevent Overview
通用 CLI 安装命令
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

讨论

登录后参与讨论。
还没有评论,来写第一条吧。

相关资产