Skip to content

bpo-32391: Implement StreamWriter.wait_closed()#5281

Merged
asvetlov merged 14 commits intopython:masterfrom
asvetlov:asyncio-streams
Jan 24, 2018
Merged

bpo-32391: Implement StreamWriter.wait_closed()#5281
asvetlov merged 14 commits intopython:masterfrom
asvetlov:asyncio-streams

Conversation

@asvetlov
Copy link
Copy Markdown
Contributor

@asvetlov asvetlov commented Jan 23, 2018

@1st1
Copy link
Copy Markdown
Member

1st1 commented Jan 23, 2018

Please go ahead and add the tests.

@asvetlov
Copy link
Copy Markdown
Contributor Author

Done, please review

Comment thread Doc/library/asyncio-stream.rst Outdated

.. coroutinemethod:: wait_closed()

Wait for writer closing.
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

-> "Wait until the writer is closed."

Comment thread Doc/library/asyncio-stream.rst Outdated

Should be called after :meth:`close` to waiting for finishing
all writer activities (:meth:`BaseProtocol.connection_lost`
callback call actually).
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

-> "Should be called after :meth:close to wait until the underlying
connection (and the associated transport/protocol pair) is closed."

Comment thread Lib/asyncio/streams.py Outdated
def __del__(self):
# Prevent reports about unhandled exceptions.
# Better than self._closed._log_traceback = False hack
if self._closed.done():
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if self._closed.done() and not self._closed.cancelled()

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ideally need a test case for the cancelled check.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed.
Don you insist on test?
I see no way to make it steady without mocking everything.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If it's so hard to hit this then it's OK. I don't like super complex mocked tests anyways.

Comment thread Lib/asyncio/streams.py Outdated

async def wait_closed(self):
if self._transport is not None:
await self._protocol._closed
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

When is the transport None? Can you add a comment?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actually I have no idea, just copied the line from drain.
Looking on source code there is no line for setting transport to None.
Let's drop the check from both methods.

Comment thread Lib/asyncio/streams.py
def close(self):
return self._transport.close()

def is_closing(self):
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this a new API method? Please document it if it is.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Forgot about the change - but exposing the method makes sense

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it's a bit too low-level. Can you imagine a good use case for async/await code that uses writer and reader?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

StreamWriter exposes all Transport methods except flow control related.
is_closing() is not about flow control, I see no reason to not implement it.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

OK, go ahead.

@@ -0,0 +1 @@
Implement :meth:`asyncio.StreamWriter.wait_closed` method
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

And is_closing() ?

@asvetlov
Copy link
Copy Markdown
Contributor Author

Please review again.

@@ -0,0 +1 @@
Implement :meth:`asyncio.StreamWriter.wait_closed` and :meth:`asyncio.StreamWriter.is_closing` methods
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

One last note: I don't think you want to use ReST formatting in NEWS entries. This would read better:

Implement wait_closed() and is_closing() methods for StreamWriter.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There is no standard for blurb text, the format of messages is mixed.
Some people use ReST, others prefer plain text.
If you briefly look on these files you'll see both cases.

I have no personal preference.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't really care.

data = self.loop.run_until_complete(f)
self.assertTrue(data.endswith(b'\r\n\r\nTest message'))
wr.close()
self.loop.run_until_complete(wr.wait_closed())
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Add assertTrue(wr.is_closing())

f = rd.read()
data = self.loop.run_until_complete(f)
self.assertTrue(data.endswith(b'\r\n\r\nTest message'))
wr.close()
Copy link
Copy Markdown
Member

@1st1 1st1 Jan 24, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Add assertFalse(wr.is_closing()) before wr.close()

@1st1
Copy link
Copy Markdown
Member

1st1 commented Jan 24, 2018

LGTM.

@asvetlov asvetlov merged commit fe133aa into python:master Jan 24, 2018
@asvetlov asvetlov deleted the asyncio-streams branch January 24, 2018 22:30
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants