Skip to content

Conversation

@tirkarthi
Copy link
Member

@tirkarthi tirkarthi commented Jun 18, 2019

  • Add docs for Stream, StreamServer, UnixStreamServer, connect and connect_unix.
  • Rewrite examples using Stream to avoid DeprecationWarning.

https://bugs.python.org/issue36889

@tirkarthi
Copy link
Member Author

Clarifications :

  • Stream object has mode that is an Enum. Does mode and enum values should be documented? StreamServer and connect that is recommended return Stream with READWRITE value.
  • Given that StreamReader and StreamWriter are merged together should I copy paste their methods to Stream?
  • StreamServer and UnixStreamServer have very similar API. I have copy pasted them but does it make sense to just redirect the user with a note to StreamServer that the methods are similar.

Thanks

Copy link
Contributor

@asvetlov asvetlov left a comment

Choose a reason for hiding this comment

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

Good job!
I have several comments/suggestions.

  1. There are connect_read_pipe() and connect_write_pipe() functions. Please document them.
  2. All public StreamServer / UnixStreamServer functions should be documented: bind(), is_bound(), abort().


This function internally uses :meth:`loop.create_connection` to return a
:class:`Stream` object of the mode ``READWRITE`` that can be used as a reader
and a writer.
Copy link
Contributor

Choose a reason for hiding this comment

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

Please reflect the fact that async with connect() can be used as well as just await connect().

Copy link
Member Author

Choose a reason for hiding this comment

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

I have added a note that connect returns a stream object which is awaitable and also about using it as context manager. Let me know if it needs to be reworded that connect itself is awaitable.

@bedevere-bot
Copy link

A Python core developer has requested some changes be made to your pull request before we can consider merging it. If you could please address their requests along with any other requests in other reviews from core developers that would be appreciated.

Once you have made the requested changes, please leave a comment on this pull request containing the phrase I have made the requested changes; please review again. I will then notify any core developers who have left a review that you're ready for them to take another look at this pull request.


writer.write(query.encode('latin-1'))
stream.write(query.encode('latin-1'))
while True:
Copy link
Member Author

Choose a reason for hiding this comment

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

Some PEP 572 related example.

while True:
    line = await stream.readline()
    if not line:
        break

    line = line.decode('latin1').rstrip()
    if line:
        print(f'HTTP header> {line}')
while (line := await stream.readline()):
    line = line.decode('latin1').rstrip()
    if line:
        print(f'HTTP header> {line}')

Copy link
Contributor

Choose a reason for hiding this comment

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

Hmm. Agree, := provides better readable code. Please apply.

@tirkarthi
Copy link
Member Author

I have made the requested changes; please review again

  • Added doc for connect_read_pipe and connect_write_pipe. I have added a note that they can be used as context manager.
  • Modified all the examples to use context managers.
  • write calls are awaited.
  • Changed enter to __aenter__ and exit to __aexit__.
  • Added all public methods to StreamServer and also updated UnixStreamServer.

Clarifications :

  • I feel StreamServer and UnixStreamServer are almost exact duplicates. Can UnixStreamServer be added a note that it provides all methods as StreamServer? The downside is that user who visits UnixStreamServer is redirected to StreamServer that there is a slight redirection overhead.
  • I have documented connect_read_pipe and connect_write_pipe similar that they return Stream object of READ and Write mode. Should we document the StreamMode Enum?

@bedevere-bot
Copy link

Thanks for making the requested changes!

@asvetlov: please review the changes made to this pull request.

Copy link
Contributor

@asvetlov asvetlov left a comment

Choose a reason for hiding this comment

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

A good step forward!
I left some comments, please fix/discuss


print('Close the connection')
await writer.close()
print('Close the connection')
Copy link
Contributor

Choose a reason for hiding this comment

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

This line is confusing, it is not followed by an actual closing code.
Maybe better to drop it, async with provides enough implication that the stream is closed on exit from the block I guess

Copy link
Member Author

Choose a reason for hiding this comment

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

Yes, the message about closing is not needed. Removed. Thanks.



.. coroutinefunction:: connect(host=None, port=None, \*, \
loop=None, limit=2**16, ssl=None, family=0, \
Copy link
Contributor

Choose a reason for hiding this comment

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

loop parameter is not supported

Copy link
Member Author

Choose a reason for hiding this comment

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

Thanks, removed.


This function internally uses :meth:`loop.create_connection` to return an awaitable
:class:`Stream` object of the mode ``READWRITE`` that can be used as a reader
and a writer. The function can also be used as an async context manager where it's
Copy link
Contributor

Choose a reason for hiding this comment

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

Just The function can also be used as an async context manager,
drop where it's automatically awaited.
Technically it's not correct: connect returns an internal undocumented object that can be awaited or used by async with. I don't want to document this object but want to provide a clear vision of how to use connect call.

Maybe two short usage examples can help?
E.g.

Connect to TCP socket on *host* : *port* address, return a read-write :class:`Stream` object.

The function can be used with ``await`` to get a connected stream::
    stream = await asyncio.connect('127.0.0.1', 8888)

Also, ``async with`` form is supported, the stream is closed on exit from the block::

   async with asyncio.connect('127.0.0.1', 8888) as stream:
        ...

   *limit* determines the buffer size limit used by the returned :class:`Stream` instance. By default the limit is set to 64 KiB.

    The rest of the arguments are passed directly to :meth:`loop.create_connection`.


I'm not sure about the exact text but I hope you've got my idea. Documentation is hard.

Copy link
Member Author

Choose a reason for hiding this comment

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

Agreed it's hard :) I have adopted your suggestion and formatted it. Did the same for connect_unix too.


.. coroutinefunction:: connect_read_pipe(pipe, *, limit=2**16)

Takes a :term:`file-like object <file object>` to return a
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
Takes a :term:`file-like object <file object>` to return a
Takes a :term:`file-like object <file object>` *pipe* to return a


.. coroutinefunction:: connect_write_pipe(pipe, *, limit=2**16)

Takes a a :term:`file-like object <file object>` to return a
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
Takes a a :term:`file-like object <file object>` to return a
Takes a a :term:`file-like object <file object>` *pipe* to return a

.. deprecated-removed:: 3.8 3.10

`open_unix_connection()` is deprecated if favor of `connect_unix()`.
`open_unix_connection()` is deprecated if favor of :func:`connect_unix`.
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
`open_unix_connection()` is deprecated if favor of :func:`connect_unix`.
``open_unix_connection()`` is deprecated if favor of :func:`connect_unix`.

.. deprecated-removed:: 3.8 3.10

`start_unix_server()` is deprecated in favor of `UnixStreamServer()`.
`start_unix_server()` is deprecated in favor of :class:`UnixStreamServer`.
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
`start_unix_server()` is deprecated in favor of :class:`UnixStreamServer`.
``start_unix_server()`` is deprecated in favor of :class:`UnixStreamServer`.

limit=2**16, family=socket.AF_UNSPEC, \
flags=socket.AI_PASSIVE, sock=None, backlog=100, \
ssl=None, reuse_address=None, reuse_port=None, \
ssl_handshake_timeout=None, shutdown_timeout=60)
Copy link
Contributor

Choose a reason for hiding this comment

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

arguments should be described here. A link to loop.create_server() can be used to don't repeat family, flags etc.

Copy link
Member Author

Choose a reason for hiding this comment

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

Added doc for client_connected_cb and limit. I have added a note that rest of the arguments are passed to loop.create_server.


.. class:: UnixStreamServer(client_connected_cb, /, path=None, *, \
limit=2**16, sock=None, backlog=100, \
ssl=None, ssl_handshake_timeout=None, shutdown_timeout=60)
Copy link
Contributor

Choose a reason for hiding this comment

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

Describe constructor arguments please

Copy link
Member Author

Choose a reason for hiding this comment

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

Added doc for client_connected_cb and limit. I have added a note that rest of the arguments are passed to loop.create_unix_server.


writer.write(query.encode('latin-1'))
stream.write(query.encode('latin-1'))
while True:
Copy link
Contributor

Choose a reason for hiding this comment

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

Hmm. Agree, := provides better readable code. Please apply.

@asvetlov
Copy link
Contributor

I feel StreamServer and UnixStreamServer are almost exact duplicates. Can UnixStreamServer be added a note that it provides all methods as StreamServer? The downside is that user who visits UnixStreamServer is redirected to StreamServer that there is a slight redirection overhead.

I don't know. I don't want to make their base class public, so duplication is probably ok. We keep references is a good shape at least.

I have documented connect_read_pipe and connect_write_pipe similar that they return Stream object of READ and Write mode. Should we document the StreamMode Enum?

Yes, please do. StreamMode is a public class

@tirkarthi
Copy link
Member Author

Changes made :

  • Documented StreamMode and it's values. Linked the mode values in relevant places like connect, connect_unix, etc.
  • Used PEP 572 in example for while loop.
  • Added doc for constructor arguments in StreamServer and UnixStreamServer and a note on rest of the arguments.
  • Added doc for limit parameter to the functions.
  • Minor doc fixes to use double backticks and added more links to relevant StreamMode docs.

Please review the changes. Thanks.

Copy link
Contributor

@asvetlov asvetlov left a comment

Choose a reason for hiding this comment

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

Awesome!

@miss-islington
Copy link
Contributor

Thanks @tirkarthi for the PR, and @asvetlov for merging it 🌮🎉.. I'm working now to backport this PR to: 3.8.
🐍🍒⛏🤖

miss-islington pushed a commit to miss-islington/cpython that referenced this pull request Jun 24, 2019
@bedevere-bot
Copy link

GH-14349 is a backport of this pull request to the 3.8 branch.

@tirkarthi
Copy link
Member Author

Thank you very much for the guidance Andrew :)

miss-islington added a commit that referenced this pull request Jun 24, 2019
lisroach pushed a commit to lisroach/cpython that referenced this pull request Sep 10, 2019
aeros added a commit to aeros/cpython that referenced this pull request Sep 27, 2019
aeros added a commit to aeros/cpython that referenced this pull request Sep 28, 2019
DinoV pushed a commit to DinoV/cpython that referenced this pull request Jan 14, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

docs Documentation in the Doc dir skip news

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants