You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: docs/quickstart.rst
+19-22Lines changed: 19 additions & 22 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -154,18 +154,17 @@ Also, one weird case is for Void types in Unions (and in general, but Void is re
154
154
155
155
bob.employment.unemployed = None
156
156
157
-
.. note:: One caveat for unions is having structs as union members. Let us assume `employment.school` was actually a struct with a field of type `Text` called `name`::
157
+
.. note:: One caveat for unions is having structs as union members. Let us assume `employment.school` was actually a struct with a field of type `Text` called `name`
158
158
159
-
alice.employment.school.name = "MIT"
160
-
# Raises a KjException
159
+
alice.employment.school.name = "MIT"
160
+
# Raises a KjException
161
161
162
-
The problem is that a struct within a union isn't initialized automatically. You have to do the following::
162
+
The problem is that a struct within a union isn't initialized automatically. You have to do the following::
163
163
164
-
TODO Broken
165
-
school = alice.employment.init('school')
166
-
school.name = "MIT"
164
+
school = alice.employment.init('school')
165
+
school.name = "MIT"
167
166
168
-
Note that this is similar to `init` for lists, but you don't pass a size. Requiring the `init` makes it more clear that a memory allocation is occurring, and will hopefully make you mindful that you shouldn't set more than 1 field inside of a union, else you risk a memory leak
167
+
Note that this is similar to `init` for lists, but you don't pass a size. Requiring the `init` makes it more clear that a memory allocation is occurring, and will hopefully make you mindful that you shouldn't set more than 1 field inside of a union, else you risk a memory leak
169
168
170
169
171
170
Writing to a File
@@ -180,14 +179,13 @@ There is also a `write_packed` function, that writes out the message more space-
180
179
Writing to a socket
181
180
~~~~~~~~~~~~~~~~~~~
182
181
Alternatively, you can write to a socket. This is useful if you want to send the message over the network or to another process.
183
-
A full example of this is available on GitHub `examples/async_socket_message_client.py <https://github.com/capnproto/pycapnp/blob/master/examples/async_socket_message_client.py>`_.
184
-
185
-
.. important:: Writing to a socket is implemented using asyncio and requires a running event loop both for the python part (asyncio) and the C++ part (KJ). See :ref:`RPC <kj-event-loop>` for more information.
186
-
::
182
+
A full example of this is available on GitHub `examples/async_socket_message_client.py <https://github.com/capnproto/pycapnp/blob/master/examples/async_socket_message_client.py>`_.::
.. important:: Writing to a socket is implemented using asyncio and requires a running event loop both for the python part (asyncio) and the C++ part (KJ). See :ref:`RPC <kj-event-loop>` for more information.
188
+
191
189
Read a message
192
190
--------------
193
191
@@ -211,14 +209,13 @@ Reading from a socket
211
209
~~~~~~~~~~~~~~~~~~~~~
212
210
213
211
The same as for writing, you can read from a socket. This is useful if you want to receive the message over the network or from another process.
214
-
A full example of this is available on GitHub `examples/async_socket_message_client.py <https://github.com/capnproto/pycapnp/blob/master/examples/async_socket_message_client.py>`_.
215
-
216
-
.. important:: Reading from a socket is implemented using asyncio and requires a running event loop both for the python part (asyncio) and the C++ part (KJ). See :ref:`RPC <kj-event-loop>` for more information.
217
-
::
212
+
A full example of this is available on GitHub `examples/async_socket_message_client.py <https://github.com/capnproto/pycapnp/blob/master/examples/async_socket_message_client.py>`_.::
.. important:: Reading from a socket is implemented using asyncio and requires a running event loop both for the python part (asyncio) and the C++ part (KJ). See :ref:`RPC <kj-event-loop>` for more information.
218
+
222
219
223
220
Reading Fields
224
221
~~~~~~~~~~~~~~
@@ -284,7 +281,7 @@ The above methods only guaranteed to work if your file contains a single message
284
281
addresses.write(f)
285
282
addresses.write(f)
286
283
addresses.write(f) # write 3 messages
287
-
284
+
288
285
with open('example.bin', 'rb') as f:
289
286
for addresses in addressbook_capnp.AddressBook.read_multiple(f):
290
287
print(addresses)
@@ -375,7 +372,7 @@ To ensure proper creation, usage, and cleanup of the KJ event loop, a context ma
375
372
async def main():
376
373
async with capnp.kj_loop():
377
374
# RPC calls here
378
-
375
+
379
376
asyncio.run(main())
380
377
381
378
To simplify the usage, the helper function:py:meth:`capnp.run` can execute a asyncio coroutine within the :py:meth:`capnp.kj_loop` context manager::
@@ -395,18 +392,18 @@ Client
395
392
396
393
Thanks to the integration into the asyncio library, most of the boiler plate code is handled by pycapnp directly. The only thing that needs to be done is to create a client object and bootstrap the server capability.
397
394
398
-
Starting a Client
395
+
Starting a Client
399
396
#################
400
397
401
-
The first step is to open a socket to the server. For now this needs to be done
398
+
The first step is to open a socket to the server. For now this needs to be done
402
399
through :py:meth:`~._AsyncIoStream.create_connection`. A thin wrapper around :py:meth:`asyncio.get_running_loop().create_connection()`
0 commit comments