or run

npx @tessl/cli init
Log in

Version

Tile

Overview

Evals

Files

docs

core-greenlets.mdindex.mdmonkey-patching.mdnetworking.mdpooling.mdqueues.mdservers.mdsynchronization.mdtimeouts.md
tile.json

tessl/pypi-gevent

A coroutine-based Python networking library that uses greenlet to provide a high-level synchronous API on top of libev event loop

Workspace
tessl
Visibility
Public
Created
Last updated
Describes
pypipkg:pypi/gevent@25.8.x

To install, run

npx @tessl/cli install tessl/pypi-gevent@25.8.0

index.mddocs/

Gevent

A coroutine-based Python networking library that uses greenlet to provide a high-level synchronous API on top of libev event loop. Gevent enables writing asynchronous, concurrent programs without explicit threading or callbacks, using cooperative lightweight threads called greenlets that yield control during I/O operations.

Package Information

  • Package Name: gevent
  • Language: Python
  • Installation: pip install gevent

Core Imports

import gevent

For specific functionality:

from gevent import socket, ssl, subprocess
from gevent import pool, queue, event, lock
from gevent import monkey, select, fileobject
from gevent import timeout, hub

Basic Usage

import gevent
from gevent import socket, monkey

# Patch standard library for cooperative behavior
monkey.patch_all()

def server(port):
    s = socket.socket()
    s.bind(('', port))
    s.listen(1000)
    
    while True:
        conn, addr = s.accept()
        gevent.spawn(handle_client, conn)

def handle_client(conn):
    try:
        data = conn.recv(1024)
        conn.send(b'HTTP/1.1 200 OK\r\n\r\nHello World')
    finally:
        conn.close()

# Start server
gevent.spawn(server, 8080).join()

Architecture

Gevent's architecture is built around several key components:

  • Greenlets: Lightweight cooperative threads that yield control during I/O operations
  • Event Loop: Underlying libev/libuv event loop that manages I/O operations
  • Hub: Central coordinator that manages greenlet scheduling and event dispatching
  • Monkey Patching: Mechanism to transparently replace standard library modules with gevent-aware versions
  • Synchronization Primitives: Cooperative equivalents of threading primitives (locks, events, queues)

This design allows writing synchronous-looking code that runs asynchronously, providing high concurrency without the complexity of traditional async programming.

Capabilities

Core Greenlet Management

Fundamental greenlet operations including spawning, waiting, killing, and coordinating multiple greenlets. These form the foundation of gevent's cooperative concurrency model.

def spawn(function, *args, **kwargs) -> Greenlet: ...
def spawn_later(seconds, function, *args, **kwargs) -> Greenlet: ...
def spawn_raw(function, *args, **kwargs) -> Greenlet: ...
def joinall(greenlets, timeout=None, raise_error=False): ...
def killall(greenlets, exception=GreenletExit, block=True, timeout=None): ...
def kill(greenlet, exception=GreenletExit, block=True, timeout=None): ...
def sleep(seconds=0): ...
def idle(priority=0): ...
def get_hub() -> Hub: ...
def getcurrent() -> Greenlet: ...
def iwait(objects, timeout=None, count=None): ...
def wait(objects, timeout=None, count=None): ...
def fork() -> int: ...
def reinit(): ...
def signal_handler(signalnum, handler): ...
def getswitchinterval() -> float: ...
def setswitchinterval(interval: float): ...

Core Greenlets

Synchronization Primitives

Thread-safe synchronization objects adapted for cooperative greenlet concurrency, including events, locks, semaphores, and async result containers.

class Event:
    def set(self): ...
    def clear(self): ...
    def wait(self, timeout=None) -> bool: ...
    def is_set(self) -> bool: ...

class AsyncResult:
    def set(self, value=None): ...
    def set_exception(self, exception): ...
    def get(self, block=True, timeout=None): ...

Synchronization

Networking and I/O

Complete networking stack with cooperative sockets, SSL support, and gevent-aware I/O operations that work seamlessly with greenlets.

def create_connection(address, timeout=None, source_address=None) -> socket: ...
def getaddrinfo(host, port, family=0, type=0, proto=0, flags=0): ...

class socket:
    def connect(self, address): ...
    def accept(self) -> tuple[socket, tuple]: ...
    def recv(self, bufsize, flags=0) -> bytes: ...
    def send(self, data, flags=0) -> int: ...

Networking

Concurrency and Pooling

Tools for managing groups of greenlets including pools with size limits, groups for coordination, and thread pools for CPU-bound operations.

class Pool:
    def __init__(self, size=None, greenlet_class=None): ...
    def spawn(self, function, *args, **kwargs) -> Greenlet: ...
    def map(self, func, iterable, maxsize=None): ...
    def imap(self, func, iterable, maxsize=None): ...

class Group:
    def add(self, greenlet): ...
    def spawn(self, function, *args, **kwargs) -> Greenlet: ...
    def join(self, timeout=None, raise_error=False): ...

Pooling

Queues and Communication

Message passing and data structures for greenlet communication including FIFO, LIFO, and priority queues with cooperative blocking behavior.

class Queue:
    def __init__(self, maxsize=None, items=None, unfinished_tasks=None): ...
    def put(self, item, block=True, timeout=None): ...
    def get(self, block=True, timeout=None): ...
    def task_done(self): ...
    def join(self, timeout=None): ...

class PriorityQueue: ...
class LifoQueue: ...

Queues

Servers and HTTP

High-performance server implementations including generic TCP/UDP servers and a complete WSGI HTTP server for web applications.

class StreamServer:
    def __init__(self, listener, handle=None, backlog=None, spawn='default', **ssl_args): ...
    def serve_forever(self, stop_timeout=None): ...
    def start(self): ...
    def stop(self, timeout=None): ...

class WSGIServer:
    def __init__(self, listener, application, **kwargs): ...
    def serve_forever(self): ...

Servers

Timeouts and Control Flow

Timeout management and control flow utilities including context managers for timeouts and cooperative equivalents of standard library functions.

class Timeout:
    def __init__(self, seconds=None, exception=None, ref=True, priority=-1): ...
    def start(self): ...
    def cancel(self): ...
    def close(self): ...
    def __enter__(self): ...
    def __exit__(self, exc_type, exc_val, exc_tb): ...
    @property
    def pending(self) -> bool: ...
    @property
    def seconds(self) -> float: ...

def with_timeout(seconds, function, *args, **kwargs): ...

Timeouts

Monkey Patching

Transparent replacement of standard library modules with gevent-aware equivalents, enabling existing code to work cooperatively without modification.

def patch_all(socket=True, dns=True, time=True, select=True, thread=True, os=True, ssl=True, subprocess=True, sys=False, aggressive=True, Event=True, builtins=True, signal=True, queue=True, contextvars=True, **kwargs): ...
def patch_socket(): ...
def patch_ssl(): ...
def patch_thread(): ...

Monkey Patching

Subprocess and System Integration

Cooperative subprocess execution and system-level integration features that work seamlessly with gevent's event loop.

class Popen:
    def __init__(self, args, **kwargs): ...
    def communicate(self, input=None, timeout=None) -> tuple[bytes, bytes]: ...
    def wait(self, timeout=None) -> int: ...
    def poll(self) -> int: ...
    def kill(self): ...
    def terminate(self): ...

def call(args, **kwargs) -> int: ...
def check_call(args, **kwargs) -> int: ...
def check_output(args, **kwargs) -> bytes: ...

File Objects and Select

Cooperative file objects and select-style operations that integrate with gevent's event loop for non-blocking I/O.

class FileObjectThread:
    def __init__(self, fobj, mode='rb', bufsize=-1, close=True): ...
    def read(self, size=-1) -> bytes: ...
    def write(self, data: bytes) -> int: ...
    def flush(self): ...
    def close(self): ...

def select(rlist, wlist, xlist, timeout=None): ...

Global Types

class Greenlet:
    def __init__(self, run=None, *args, **kwargs): ...
    def start(self): ...
    def start_later(self, seconds): ...
    def join(self, timeout=None): ...
    def kill(self, exception=GreenletExit, block=True, timeout=None): ...
    def get(self, block=True, timeout=None): ...
    def throw(self, *args): ...
    def ready(self) -> bool: ...
    def successful(self) -> bool: ...
    def rawlink(self, callback): ...
    def link(self, callback): ...
    def unlink(self, callback): ...
    def unlink_all(self): ...
    def link_value(self, callback): ...
    def link_exception(self, callback): ...
    @classmethod
    def spawn(cls, *args, **kwargs) -> 'Greenlet': ...
    @classmethod
    def spawn_later(cls, seconds, *args, **kwargs) -> 'Greenlet': ...
    @staticmethod
    def add_spawn_callback(callback): ...
    @staticmethod
    def remove_spawn_callback(callback): ...
    @property
    def started(self) -> bool: ...
    @property
    def dead(self) -> bool: ...
    @property
    def name(self) -> str: ...
    @property
    def minimal_ident(self) -> int: ...
    @property
    def exception(self): ...
    @property
    def exc_info(self): ...

class GreenletExit(BaseException): ...

class Hub:
    def switch(self): ...
    def run(self): ...
    def stop(self): ...
    def destroy(self): ...
    def join(self, timeout=None): ...
    @property
    def loop(self): ...