Skip to content

Commit

Permalink
Add an example of using "await" instead of "async with"
Browse files Browse the repository at this point in the history
This commit adds an example of using "await" instead of "async with"
when opening a new SSHClientConnection. This is useful when you have
code which wants to open a connection and return it without using it
right away. When using "async with", the connection is automatically
closed when the block is exited. Thanks go to Michael Davis for
suggesting this added documentation.
  • Loading branch information
ronf committed Mar 6, 2024
1 parent 21a3062 commit a3c3fce
Showing 1 changed file with 20 additions and 1 deletion.
21 changes: 20 additions & 1 deletion docs/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,26 @@ key doesn't match.
:literal:
:start-line: 22

This example only uses the output on stdout, but output on stderr is also
This example shows using the :class:`SSHClientConnection` returned by
:func:`connect()` as a context manager, so that the connection is
automatically closed when the end of the code block which opened it is
reached. However, if you need the connection object to live longer, you
can use "await" instead of "async with":

.. code::
conn = await asyncssh.connect('localhost')
In this case, the application will need to close the connection explicitly
when done with it, and it is best to also wait for the close to complete.
This can be done with the following code from inside an async function:

.. code::
conn.close()
await conn.wait_closed()
Only stdout is referenced this example, but output on stderr is also
collected as another attribute in the returned :class:`SSHCompletedProcess`
object.

Expand Down

0 comments on commit a3c3fce

Please sign in to comment.