Python Glossary Русский

all Python 3until 3.12Removed from the standard libraryterm

asyncore

removed modules

Module for asynchronous sockets built on select() with the dispatcher class. Removed in Python 3.12; use asyncio instead.

Syntax

import asyncore  # до 3.12

asyncore was the base of the old select()-based event framework: a subclass of asyncore.dispatcher overrode handle_read() and handle_write(), and asyncore.loop() ran the loop. asyncio replaced it, so the module was deprecated in Python 3.6 and removed in 3.12 under PEP 594. How to rewrite: instead of dispatcher and loop() write a handler coroutine with StreamReader and StreamWriter, start a server with asyncio.start_server(), a client with asyncio.open_connection(), and run it with asyncio.run(). The pyasyncore package on PyPI brings the old module back, but write new code with asyncio.

Examples

# Python 3.12+
try:
    import asyncore
except ModuleNotFoundError as e:
    print(type(e).__name__)  # → ModuleNotFoundError
# Python 3.12+
import asyncio, socket
async def main():
    a, b = socket.socketpair()
    reader, writer = await asyncio.open_connection(sock=a)
    peer_reader, peer_writer = await asyncio.open_connection(sock=b)
    writer.write(b'ping')
    await writer.drain()
    data = await peer_reader.read(4)
    print(data)  # → b'ping'
    writer.close()
    peer_writer.close()
asyncio.run(main())

See also

Common errors

Open in the full glossarydocs.python.orgReport an inaccuracy