Replace asyncore with asyncio for better support #3264
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Pull Request Description
Title: Migration from
asyncore
toasyncio
for Compatibility with Python 3.12 and AboveDescription:
This pull request migrates the
AsyncoreConnectionDispatcher
class from using the deprecatedasyncore
module to the modernasyncio
module. Theasyncore
module has been removed in Python 3.12, necessitating this update to ensure compatibility with current and future versions of Python.Changes Made:
Module Replacement:
asyncore
withasyncio
.Class Definition:
asyncore.dispatcher_with_send
.YowConnectionDispatcher
.Initialization:
super().__init__()
for proper initialization in Python 3.asyncio
event loop withself.loop = asyncio.get_event_loop()
.self.sock = None
to maintain a reference to the socket.sendData Method:
_send_data
.self.sock.sendall(data)
to send data over the socket.asyncio.run(self._send_data(data))
insendData
.connect Method:
_connect
.await self.loop.sock_connect(self.sock, host)
for the connection.self.loop.run_until_complete(self._connect(host))
to run the coroutine inconnect
.handle_connect Method:
handle_close Method:
handle_error Method:
handle_read Method:
_handle_read
.await self.loop.sock_recv(self.sock, 1024)
for non-blocking read.asyncio.run(self._handle_read())
inhandle_read
.disconnect Method:
Why These Changes Are Required:
Deprecation of
asyncore
: Theasyncore
module has been deprecated and removed in Python 3.12. This necessitates the migration toasyncio
, which is the modern framework for asynchronous I/O operations in Python.Future Compatibility: Ensuring that the codebase remains compatible with future versions of Python is crucial for long-term maintenance and support. Migrating to
asyncio
aligns with current Python standards.Enhanced Asynchronous Support:
asyncio
provides a more robust and flexible framework for asynchronous programming, with better performance and more features compared toasyncore
.Testing and Verification:
asyncio
.AsyncoreConnectionDispatcher
class have been preserved, ensuring backward compatibility with other parts of the codebase that rely on this class.Conclusion:
This pull request updates the
AsyncoreConnectionDispatcher
class to useasyncio
, ensuring compatibility with Python 3.12 and above while maintaining existing functionalities and structure. This migration is essential for the long-term maintenance and support of the codebase, aligning it with modern Python standards for asynchronous programming.