Debian Codegraph @

docs/tutorial.rst

raw
1 .. _tutorial:
2
3 Tutorial
4 ========
5
6
7 Basics
8 ------
9
10 Using :mod:`pyxs` is easy! The only class you need to import is
11 :class:`~pyxs.client.Client`. It provides a simple straightforward API
12 to XenStore content with a bit of Python's syntactic sugar here and
13 there.
14
15 Generally, if you just need to fetch or update some XenStore items you
16 can do::
17
18 >>> from pyxs import Client
19 >>> with Client() as c:
20 ... c[b"/local/domain/0/name"] = b"Ziggy"
21 ... c[b"/local/domain/0/name"]
22 b'Ziggy'
23
24 Using :class:`~pyxs.client.Client` without the ``with`` statement is
25 possible, albeit, not recommended:
26
27 >>> c = Client()
28 >>> c.connect()
29 >>> c[b"/local/domain/0/name"] = b"It works!"
30 >>> c.close()
31
32 The reason for preferring a context manager is simple: you don't have
33 to DIY. The context manager will make sure that a started transaction
34 was either rolled back or committed and close the underlying XenStore
35 connection afterwards.
36
37 Connections
38 -----------
39
40 :mod:`pyxs` supports two ways of communicating with XenStore:
41
42 * over a Unix socket with :class:`~pyxs.connection.UnixSocketConnection`;
43 * over XenBus_ with :class:`~pyxs.connection.XenBusConnection`.
44
45 Connection type is determined from the arguments passed to
46 :class:`~pyxs.client.Client` constructor. For example, the
47 following code creates a :class:`~pyxs.client.Client` instance,
48 operating over a Unix socket::
49
50 >>> Client(unix_socket_path="/var/run/xenstored/socket_ro")
51 Client(UnixSocketConnection('/var/run/xenstored/socket_ro'))
52 >>> Client()
53 Client(UnixSocketConnection('/var/run/xenstored/socket'))
54
55 Use ``xen_bus_path`` argument to initialize a :class:`~pyxs.client.Client` with
56 :class:`~pyxs.connection.XenBusConnection`::
57
58 >>> Client(xen_bus_path="/dev/xen/xenbus")
59 Client(XenBusConnection('/dev/xen/xenbus'))
60
61 .. _XenBus: http://wiki.xensource.com/xenwiki/XenBus
62
63 Transactions
64 ------------
65
66 Transactions allow you to operate on an isolated copy of XenStore tree
67 and merge your changes back atomically on commit. Keep in mind, however,
68 that changes made within a transaction become available to other XenStore
69 clients only if and when committed. Here's an example::
70
71 >>> with Client() as c:
72 ... c.transaction()
73 ... c[b"/foo/bar"] = b"baz"
74 ... c.commit() # !
75 ... print(c[b"/foo/bar"])
76 b'baz'
77
78 The line with an exclamation mark is a bit careless, because it
79 ignores the fact that committing a transaction might fail. A more
80 robust way to commit a transaction is by using a loop::
81
82 >>> with Client() as c:
83 ... success = False
84 ... while not success:
85 ... c.transaction()
86 ... c[b"/foo/bar"] = b"baz"
87 ... success = c.commit()
88
89 You can also abort the current transaction by calling
90 :meth:`~pyxs.client.Client.rollback`.
91
92 Events
93 ------
94
95 When a new path is created or an existing path is modified, XenStore
96 fires an event, notifying all watching clients that a change has been
97 made. :mod:`pyxs` implements watching via the :class:`Monitor`
98 class. To watch a path create a monitor
99 :meth:`~pyxs.client.Client.monitor` and call
100 :meth:`~pyxs.client.Monitor.watch` with a path you want to watch and a
101 unique token. Right after that the monitor will start to accumulate
102 incoming events. You can iterate over them via
103 :meth:`~pyxs.client.Monitor.wait`::
104
105 >>> with Client() as c:
106 ... m = c.monitor()
107 ... m.watch(b"/foo/bar", b"a unique token")
108 ... next(m.wait())
109 Event(b"/foo/bar", b"a unique token")
110
111 XenStore has a notion of *special* paths, which start with ``@`` and
112 are reserved for special occasions:
113
114 ================ ================================================
115 Path Description
116 ---------------- ------------------------------------------------
117 @introduceDomain Fired when a **new** domain is introduced to
118 XenStore -- you can also introduce domains
119 yourself with a
120 :meth:`~pyxs.client.Client.introduce_domain`
121 call, but in most of the cases, ``xenstored``
122 will do that for you.
123 @releaseDomain Fired when XenStore is no longer communicating
124 with a domain, see
125 :meth:`~pyxs.client.Client.release_domain`.
126 ================ ================================================
127
128 Events for both special and ordinary paths are simple two element
129 tuples, where the first element is always `event target` -- a path
130 which triggered the event and second is a token passed to
131 :meth:`~pyxs.client.Monitor.watch`. A rather unfortunate consequence
132 of this is that you can't get `domid` of the domain, which triggered
133 @introduceDomain or @releaseDomain from the received event.
134
135
136 Compatibility API
137 -----------------
138
139 :mod:`pyxs` also provides a compatibility interface, which mimics that
140 of ``xen.lowlevel.xs`` --- so you don't have to change
141 anything in the code to switch to :mod:`pyxs`::
142
143 >>> from pyxs import xs
144 >>> handle = xs()
145 >>> handle.read("0", b"/local/domain/0/name")
146 b'Domain-0'
147 >>> handle.close()