Skip to content

Commit

Permalink
Update documentation to async code (capnproto#331)
Browse files Browse the repository at this point in the history
This commit updates the documentation to the latest changes added
with pycapnp 2.0.0
  • Loading branch information
tobiasah committed Oct 12, 2023
1 parent ae96512 commit 378a967
Show file tree
Hide file tree
Showing 5 changed files with 117 additions and 281 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ While not directly supported by pycapnp, a tool has been created to help generat

## Python Versions

Python 3.7+ is supported.
Python 3.8+ is supported.


## Development
Expand Down
32 changes: 26 additions & 6 deletions capnp/lib/capnp.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -438,8 +438,10 @@ cdef class _DynamicListReader:
cdef class _DynamicResizableListBuilder:
"""Class for building growable Cap'n Proto Lists
.. warning:: You need to call :meth:`finish` on this object before serializing the Cap'n Proto message.
Failure to do so will cause your objects not to be written out as well as leaking orphan structs into your message.
.. warning::
You need to call :meth:`finish` on this object before serializing the
Cap'n Proto message. Failure to do so will cause your objects not to be
written out as well as leaking orphan structs into your message.
This class works much like :class:`_DynamicListBuilder`, but it allows growing the list dynamically.
It is meant for lists of structs, since for primitive types like int or float, you're much better off
Expand Down Expand Up @@ -1436,9 +1438,11 @@ cdef class _DynamicStructBuilder:
This is only meant for lists of Cap'n Proto objects, since for primitive types
you can just define a normal python list and fill it yourself.
.. warning:: You need to call :meth:`_DynamicResizableListBuilder.finish` on the
list object before serializing the Cap'n Proto message. Failure to do so will cause
your objects not to be written out as well as leaking orphan structs into your message.
.. warning::
You need to call :meth:`_DynamicResizableListBuilder.finish` on the
list object before serializing the Cap'n Proto message. Failure to do
so will cause your objects not to be written out as well as leaking
orphan structs into your message.
:type field: str
:param field: The field name to initialize
Expand Down Expand Up @@ -1850,6 +1854,16 @@ cdef class _EventLoop:

@_asynccontextmanager
async def kj_loop():
"""Context manager for running the KJ event loop
As long as the context manager is active it is guaranteed that the KJ event
loop is running. When the context manager is exited, the KJ event loop is
shut down properly and pending tasks are cancelled.
:raises [RuntimeError]: If the KJ event loop is already running (on this thread).
.. warning:: Every capnp rpc call required a running KJ event loop.
"""
asyncio_loop = asyncio.get_running_loop()
if hasattr(asyncio_loop, '_kj_loop'):
raise RuntimeError("The KJ event-loop is already running (on this thread).")
Expand All @@ -1876,6 +1890,12 @@ async def kj_loop():
kj_loop.close()

async def run(coro):
"""Ensure that the coroutine runs while the KJ event loop is running
This is a shortcut for wrapping the coroutine in a :py:meth:`capnp.kj_loop` context manager.
:param coro: Coroutine to run
"""
async with kj_loop():
return await coro

Expand Down Expand Up @@ -4391,7 +4411,7 @@ def add_import_hook(additional_paths=[]):
:type additional_paths: list
:param additional_paths: Additional paths, listed as strings, to be used to search for the .capnp files.
It is prepended to the beginning of sys.path. It also affects imports inside of Cap'n Proto schemas.
It is prepended to the beginning of sys.path. It also affects imports inside of Cap'n Proto schemas.
"""
global _importer
if _importer is not None:
Expand Down
44 changes: 6 additions & 38 deletions docs/capnp.rst
Original file line number Diff line number Diff line change
Expand Up @@ -14,36 +14,6 @@ Classes
RPC
~~~

Promise
#######

.. autoclass:: Promise
:members:
:undoc-members:
:inherited-members:

Promise may be one of:

* :meth:`capnp.lib.capnp._Promise`
* :meth:`capnp.lib.capnp._RemotePromise`
* :meth:`capnp.lib.capnp._VoidPromise`

.. autoclass:: capnp.lib.capnp._Promise
:members:
:undoc-members:
:inherited-members:

.. autoclass:: capnp.lib.capnp._RemotePromise
:members:
:undoc-members:
:inherited-members:

.. autoclass:: capnp.lib.capnp._VoidPromise
:members:
:undoc-members:
:inherited-members:


Communication
#############

Expand Down Expand Up @@ -96,16 +66,14 @@ Miscellaneous
Functions
---------
.. autofunction:: add_import_hook
.. autofunction:: remove_import_hook
.. autofunction:: cleanup_global_schema_parser
.. autofunction:: create_event_loop
.. autofunction:: getTimer
.. autofunction:: join_promises

.. autofunction:: kj_loop
.. autofunction:: run

.. autofunction:: load
.. autofunction:: poll_once
.. autofunction:: remove_event_loop
.. autofunction:: remove_import_hook
.. autofunction:: reset_event_loop
.. autofunction:: wait_forever



Internal Classes
Expand Down
2 changes: 1 addition & 1 deletion docs/conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -307,6 +307,6 @@
# Allow duplicate toc entries.
# epub_tocdup = True

intersphinx_mapping = {"http://docs.python.org/": None}
intersphinx_mapping = {"<name>": ("http://docs.python.org/", None)}

smv_branch_whitelist = r"^master$"
Loading

0 comments on commit 378a967

Please sign in to comment.