{"id":6423,"library":"pyasynchat","title":"pyasynchat","description":"pyasynchat is a re-implementation of Python's `asynchat` standard library module, which was removed in Python 3.12. It provides a compatible API for asynchronous event-driven network communication, built upon `pyasyncore`. The current version is 1.0.5, and releases are made as needed to ensure compatibility with newer Python versions or address bugs.","status":"active","version":"1.0.5","language":"en","source_language":"en","source_url":"https://github.com/simonrob/pyasynchat","tags":["networking","async","compatibility","python3.12","asyncore"],"install":[{"cmd":"pip install pyasynchat","lang":"bash","label":"Install pyasynchat"},{"cmd":"pip install pyasynchat pyasyncore","lang":"bash","label":"Install with its core dependency"}],"dependencies":[{"reason":"pyasynchat is built on pyasyncore and requires it for core asynchronous event loop functionality.","package":"pyasyncore","optional":false}],"imports":[{"note":"The original `asynchat` module was removed in Python 3.12+. This library provides the functionality under its own namespace.","wrong":"from asynchat import async_chat","symbol":"async_chat","correct":"from pyasynchat import async_chat"},{"note":"The original `asynchat` module was removed in Python 3.12+. This library provides the functionality under its own namespace.","wrong":"from asynchat import simple_producer","symbol":"simple_producer","correct":"from pyasynchat import simple_producer"}],"quickstart":{"code":"import socket\nimport pyasyncore\nfrom pyasynchat import async_chat, simple_producer # simple_producer is imported but not used for brevity in this example\n\nclass EchoHandler(async_chat):\n    def __init__(self, sock):\n        super().__init__(sock)\n        self.set_terminator(b'\\n')\n        self.data = []\n\n    def collect_incoming_data(self, data):\n        self.data.append(data)\n\n    def found_terminator(self):\n        message = b\"\".join(self.data).strip()\n        print(f\"Received: {message.decode()}\")\n        self.push(message + b'\\n') # Echo back with a newline\n        self.data = []\n\nclass EchoServer(pyasyncore.dispatcher):\n    def __init__(self, host, port):\n        super().__init__()\n        self.create_socket(socket.AF_INET, socket.SOCK_STREAM)\n        self.set_reuse_addr()\n        self.bind((host, port))\n        self.listen(5)\n        print(f\"Listening on {host}:{port}\")\n\n    def handle_accept(self):\n        pair = self.accept()\n        if pair is not None:\n            sock, addr = pair\n            print(f\"Incoming connection from {addr}\")\n            EchoHandler(sock)\n\nif __name__ == \"__main__\":\n    # The loop function must be imported from pyasyncore, not pyasynchat.\n    # Ensure pyasyncore is also installed: pip install pyasyncore\n    server = EchoServer('localhost', 8080)\n    try:\n        pyasyncore.loop()\n    except KeyboardInterrupt:\n        print(\"Server stopped.\")\n\n# To test: In another terminal, run: nc localhost 8080","lang":"python","description":"This quickstart demonstrates a simple asynchronous echo server using `pyasynchat` for handling client communication and `pyasyncore` for the main event loop. It mirrors the functionality of a classic `asynchat` example."},"warnings":[{"fix":"Migrate applications to use `pyasynchat` (and `pyasyncore`) as external packages.","message":"The original `asynchat` standard library module was removed from Python 3.12 and later versions.","severity":"breaking","affected_versions":"Python 3.12+"},{"fix":"Change `import asynchat` to `import pyasynchat` or `from asynchat import ...` to `from pyasynchat import ...`.","message":"Applications using `asynchat` must update their import statements to use `pyasynchat`.","severity":"breaking","affected_versions":"Python 3.12+"},{"fix":"Ensure `pip install pyasynchat pyasyncore` is run, and update any `asyncore.loop()` calls to `pyasyncore.loop()` if `asyncore` itself was previously imported directly.","message":"`pyasynchat` relies on `pyasyncore` for the underlying asynchronous event loop (`asyncore.loop`). Both packages must be installed for `pyasynchat` to function correctly.","severity":"gotcha","affected_versions":"All versions of `pyasynchat`"},{"fix":"Thoroughly test existing applications after migration to `pyasynchat` and `pyasyncore` to ensure no unexpected behavior changes.","message":"As a re-implementation of a removed standard library module, while aiming for high compatibility, subtle behavioral differences might exist compared to the original CPython standard library versions of `asynchat` and `asyncore`.","severity":"gotcha","affected_versions":"All versions"}],"env_vars":null,"last_verified":"2026-04-15T00:00:00.000Z","next_check":"2026-07-14T00:00:00.000Z"}