Skip to content

Commit 0c6089e

Browse files
authored
Merge pull request #93 from YuanYuYuan/fix/python-example
fix: wrap zenoh-python sessions in context manager
2 parents c2f9c8e + f7c034d commit 0c6089e

File tree

1 file changed

+28
-29
lines changed

1 file changed

+28
-29
lines changed

content/docs/getting-started/first-app.md

Lines changed: 28 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,10 @@ Let us take a step-by-step approach in putting together your first Zenoh applica
1010
As the first step, let us see how we get some data from a temperature sensor in our kitchen.
1111
Then we see how we can route this data to store and perform some analytics.
1212

13-
Before cranking some code, let's define some terminology.
13+
Before cranking some code, let's define some terminology.
1414

1515
<b>Zenoh</b> deals with <i>keys/values</i> where each key is a <i>path</i> and is associated to a <i>value</i>. A key looks like just a Unix file system path, such as ```myhome/kitchen/temp```. The value can be defined with different
16-
encodings (string, JSON, raw bytes buffer...).
16+
encodings (string, JSON, raw bytes buffer...).
1717
<!-- To query the values stored by Zenoh, we use <i>selectors</i>. As the name suggest, a <i>selector</i> can use wildcards, such as <b>*</b> and <b>**</b> to represent a set of paths, such as, ```myhome/*/temp```. -->
1818

1919
Let's get started!
@@ -37,15 +37,15 @@ def read_temp():
3737
return random.randint(15, 30)
3838

3939
if __name__ == "__main__":
40-
session = zenoh.open(zenoh.Config())
41-
key = 'myhome/kitchen/temp'
42-
pub = session.declare_publisher(key)
43-
while True:
44-
t = read_temp()
45-
buf = f"{t}"
46-
print(f"Putting Data ('{key}': '{buf}')...")
47-
pub.put(buf)
48-
time.sleep(1)
40+
with zenoh.open(zenoh.Config()) as session:
41+
key = 'myhome/kitchen/temp'
42+
pub = session.declare_publisher(key)
43+
while True:
44+
t = read_temp()
45+
buf = f"{t}"
46+
print(f"Putting Data ('{key}': '{buf}')...")
47+
pub.put(buf)
48+
time.sleep(1)
4949
```
5050

5151
Now we need a subscriber, `z_subscriber.py` that can receive the measurements:
@@ -55,19 +55,19 @@ import zenoh, time
5555

5656
def listener(sample):
5757
print(f"Received {sample.kind} ('{sample.key_expr}': '{sample.payload.to_string()}')")
58-
58+
5959
if __name__ == "__main__":
60-
session = zenoh.open(zenoh.Config())
61-
sub = session.declare_subscriber('myhome/kitchen/temp', listener)
62-
time.sleep(60)
60+
with zenoh.open(zenoh.Config()) as session:
61+
sub = session.declare_subscriber('myhome/kitchen/temp', listener)
62+
time.sleep(60)
6363
```
6464

6565
Start the subscriber:
6666
```bash
6767
python3 z_subscriber.py
6868
```
6969
The subscriber waits for an update on `myhome/kitchen/temp`.
70-
70+
7171
Now start `z_sensor.py` as follows
7272
```bash
7373
python3 z_sensor.py
@@ -78,7 +78,7 @@ You can see the values produced by the sensor being consumed by the subscriber.
7878
## Store and Query in Zenoh
7979

8080
As the next step, let's see how the value generated by a publisher can be stored in Zenoh.
81-
For this, we use [Zenoh router](../installation) (`zenohd`).
81+
For this, we use [Zenoh router](../installation) (`zenohd`).
8282
By default, a Zenoh router starts without any storage. In order to store the temperature, we need to configure one.
8383
Create a `zenoh-myhome.json5` configuration file for Zenoh with this content:
8484
```json5
@@ -107,24 +107,23 @@ Create a `zenoh-myhome.json5` configuration file for Zenoh with this content:
107107
zenohd -c zenoh-myhome.json5
108108
```
109109

110-
Now the data generated by our temperature sensor is stored in memory.
110+
Now the data generated by our temperature sensor is stored in memory.
111111
We can retrieve the latest temperature value stored in Zenoh:
112112

113113
```python
114114
import zenoh
115115

116116
if __name__ == "__main__":
117-
session = zenoh.open()
118-
replies = session.get('myhome/kitchen/temp')
119-
for reply in replies:
120-
try:
121-
print("Received ('{}': '{}')"
122-
.format(reply.ok.key_expr, reply.ok.payload.to_string()))
123-
except:
124-
print("Received (ERROR: '{}')"
125-
.format(reply.err.payload.to_string()))
126-
127-
session.close()
117+
with zenoh.open(zenoh.Config()) as session:
118+
replies = session.get('myhome/kitchen/temp')
119+
for reply in replies:
120+
try:
121+
print("Received ('{}': '{}')"
122+
.format(reply.ok.key_expr, reply.ok.payload.to_string()))
123+
except:
124+
print("Received (ERROR: '{}')"
125+
.format(reply.err.payload.to_string()))
126+
128127
```
129128
## Other examples
130129

0 commit comments

Comments
 (0)