Eventlet

0.41.0 · deprecated · verified Thu Apr 09

Eventlet is a concurrent networking library for Python that uses coroutines (greenlets) and non-blocking I/O (epoll/libevent) to enable blocking-style programming in a highly scalable, asynchronous environment. As of version 0.41.0, Eventlet is in maintenance mode, discouraging new projects from using it and planning for its eventual retirement in favor of `asyncio` due to compatibility issues with newer Python versions and a growing gap in maintenance.

Warnings

Install

Imports

Quickstart

This quickstart demonstrates Eventlet's core functionality: monkey-patching the standard library for non-blocking I/O and spawning greenlets for concurrent execution. It fetches multiple URLs concurrently and then shows cooperative sleeping.

import eventlet
from eventlet.green import urllib.request
import time

eventlet.monkey_patch()

def fetch_url(url):
    print(f"Fetching {url}...")
    try:
        # urllib.request becomes non-blocking after monkey_patch()
        response = urllib.request.urlopen(url)
        data = response.read()
        print(f"Finished fetching {url}, size: {len(data)} bytes")
    except Exception as e:
        print(f"Error fetching {url}: {e}")

if __name__ == '__main__':
    urls = [
        'http://www.google.com',
        'http://www.bing.com',
        'http://www.yahoo.com',
    ]

    print("Starting concurrent fetches...")
    pool = eventlet.GreenPool()
    for url in urls:
        pool.spawn(fetch_url, url)
    pool.waitall()
    print("All fetches complete.")

    print("\nDemonstrating concurrent sleep:")
    def greet(name, delay):
        eventlet.sleep(delay) # Cooperative sleep
        print(f"Hello, {name} after {delay} seconds!")

    pool_sleep = eventlet.GreenPool()
    pool_sleep.spawn(greet, "Alice", 2)
    pool_sleep.spawn(greet, "Bob", 1)
    pool_sleep.waitall()
    print("All greetings complete.")

view raw JSON →