-
-
Notifications
You must be signed in to change notification settings - Fork 32.1k
asyncio.transports.BaseTransport: Add payload to close #103338
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
What's the use case for it? |
Explicit control over why a |
I'm sorry, but you have to give more detail about the use case you have in mind. |
Sure thing. Following the example from https://docs.python.org/3/library/asyncio-protocol.html#connecting-existing-sockets class IntentionalClose(Exception):
...
class ClientProtocol(Protocol):
def __init__(self, connection_lost_cb: Callable) -> None:
self.connection_lost_callback = connection_lost_cb
super().__init__()
def connection_lost(self, exc: Exception | None) -> None:
# Here exc is None when the *remote* closes gracefully with EOF
if exc is IntentionalClose:
return
self.connection_lost_callback()
async def main() -> None:
def reconnect() -> asyncio.Task:
return asyncio.create_task(connect)
async def connect() -> None:
loop = asyncio.get_running_loop()
transport, protocol = await loop.create_connection(
lambda: ClientProtocol(reconnect), "127.0.0.1", 8888
)
await asyncio.sleep(20.0)
transport.close(IntentionalClose) This interaction is explicit and avoids the |
|
That example is incomplete, but more seriously, I don't think that it counts as a "use case". A use case is typically a story (not code!) about a real-world problem that you experienced that leads you to propose the new feature, because the problem cannot be solved without the new feature (or the solution is complicated, or particularly error-prone). It looks to me like you want to distinguish in your protocol between a clean disconnect by the other end of the socket, and a |
I don't want to further extend the transport implementation without a strong use-case hence closing. |
Uh oh!
There was an error while loading. Please reload this page.
Feature or enhancement
Calling
Transport.close
maybe callsconnection_lost
when closed.To e.g. gracefully close connections it would be helpful to transport an exception payload to
connection_lost
. Which ifexc
is present, will always callProtocol.connection_lost
.Pitch
Implement
Protocol.close
to take an exception argument:This will yield more control and guards to closing the connection.
The text was updated successfully, but these errors were encountered: