{"id":4825,"library":"types-gevent","title":"Typing Stubs for gevent","description":"types-gevent provides external type annotations (stubs) for the gevent library. It enables static type checkers like MyPy and Pyright to perform type checking, inference, and autocompletion for code that uses gevent. As part of the typeshed project, its releases are automatically generated and follow a versioning scheme tied to the gevent runtime package version.","status":"active","version":"26.4.0.20260409","language":"en","source_language":"en","source_url":"https://github.com/python/typeshed","tags":["typing","stubs","gevent","typeshed","async","concurrency","type-hints"],"install":[{"cmd":"pip install types-gevent","lang":"bash","label":"Install stubs"}],"dependencies":[{"reason":"types-gevent provides type hints for the gevent library; gevent is required at runtime for any practical use of the stubs.","package":"gevent","optional":false}],"imports":[{"note":"types-gevent itself is not imported directly; it provides type information for imports from the 'gevent' library.","symbol":"gevent","correct":"import gevent"},{"note":"Used for applying monkey patches to standard library modules.","symbol":"gevent.monkey","correct":"import gevent.monkey"}],"quickstart":{"code":"import gevent\nimport gevent.monkey\nimport typing\n\ngevent.monkey.patch_all()\n\ndef fetch_data(url: str) -> str:\n    \"\"\"Simulates fetching data asynchronously.\"\"\"\n    gevent.sleep(0.1) # Simulate I/O delay\n    return f\"Data from {url}\"\n\ndef process_urls(urls: typing.List[str]) -> typing.List[str]:\n    greenlets = [gevent.spawn(fetch_data, url) for url in urls]\n    gevent.joinall(greenlets)\n    results = [g.value for g in greenlets]\n    return results\n\nif __name__ == \"__main__\":\n    urls_to_process = [\"http://example.com/1\", \"http://example.com/2\"]\n    processed_data = process_urls(urls_to_process)\n    print(f\"Processed: {processed_data}\")\n\n# To run type checking, save this as e.g., 'main.py' and run:\n# mypy main.py","lang":"python","description":"This example demonstrates how types-gevent provides type hints for the gevent library. After installing both 'gevent' and 'types-gevent', a type checker like MyPy can analyze the code. The type hints for `fetch_data` and `process_urls` will be checked, and `gevent.spawn` and `gevent.joinall` will have their expected types available for static analysis."},"warnings":[{"fix":"Ensure that the major and minor version numbers of `types-gevent` (e.g., '25.9' from '25.9.0.20260408') match or are compatible with your installed `gevent` runtime version. Pin both `gevent` and `types-gevent` to compatible versions in your project's dependencies.","message":"Type stubs are version-specific. Installing 'types-gevent' for 'gevent==X.Y' alongside a different version of 'gevent' (e.g., 'gevent==A.B' where A.B != X.Y) can lead to incorrect type checking errors or missed type issues. The 'types-gevent' package version `X.Y.Z.YYYYMMDD` indicates it targets `gevent==X.Y.*`.","severity":"breaking","affected_versions":"All versions"},{"fix":"Always install the 'gevent' runtime library (`pip install gevent`) in addition to its type stubs (`pip install types-gevent`) for your application to function correctly.","message":"Stub packages like 'types-gevent' provide static type information only; they do not include any runtime code or functionality. Installing 'types-gevent' without the actual 'gevent' library will not make 'gevent' available for execution.","severity":"gotcha","affected_versions":"All versions"},{"fix":"Place `gevent.monkey.patch_all()` at the top of your application's entry point file before any other imports that might use standard library I/O or concurrency primitives.","message":"When using `gevent.monkey.patch_all()`, the patching must occur as early as possible in your application's lifecycle, typically at the very beginning of your main script. Delaying patching, especially after other standard library modules (e.g., `concurrent.futures`, `threading`) have been imported, can result in those modules retaining their original blocking behavior or lead to conflicts.","severity":"gotcha","affected_versions":"All versions of gevent utilizing monkey patching"},{"fix":"Offload CPU-intensive operations to separate processes or threads (e.g., using `multiprocessing` or `concurrent.futures.ThreadPoolExecutor` where appropriate) to avoid blocking the gevent event loop.","message":"Gevent is designed for I/O-bound concurrency, not CPU-bound parallelism. Running CPU-intensive tasks within a greenlet will block the entire event loop, preventing other greenlets from executing and negating the benefits of cooperative multitasking.","severity":"gotcha","affected_versions":"All versions"},{"fix":"If multiprocessing is necessary, carefully review the `gevent` documentation on `gevent.subprocess` and `multiprocessing` interactions. Consider using process pools or alternative strategies for multi-core utilization that are compatible with `gevent`'s cooperative concurrency model.","message":"Combining `gevent` with Python's built-in `multiprocessing` module can introduce complex issues and is generally discouraged without careful management. Specifically, after forking on POSIX systems, `gevent`'s internal state in the child process can be ill-posed, leading to unexpected behavior.","severity":"gotcha","affected_versions":"All versions"}],"env_vars":null,"last_verified":"2026-04-12T00:00:00.000Z","next_check":"2026-07-11T00:00:00.000Z"}