-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathchannel_cleanup_example.py
More file actions
249 lines (190 loc) · 6.98 KB
/
Copy pathchannel_cleanup_example.py
File metadata and controls
249 lines (190 loc) · 6.98 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
#!/usr/bin/env python3
"""
Example: Proper Channel Cleanup Patterns
This example demonstrates best practices for removing channels when done
to prevent radiod from accumulating unused channel instances.
IMPORTANT: Always remove channels in your downstream applications!
NOTE: Channel removal is ASYNCHRONOUS. Setting frequency to 0 marks the
channel for removal, and radiod periodically polls to remove marked channels.
The channel may still appear in discovery for a brief time after calling
remove_channel(). This is normal radiod behavior.
Reference: https://github.com/ka9q/ka9q-radio
"""
import sys
import time
from pathlib import Path
# Add parent directory to path
sys.path.insert(0, str(Path(__file__).parent.parent))
from ka9q import RadiodControl
def pattern1_context_manager():
"""
Pattern 1: Using Context Manager (Recommended)
The context manager automatically closes the connection, but you
still need to explicitly remove channels before exiting.
"""
print("=" * 60)
print("Pattern 1: Context Manager with Explicit Cleanup")
print("=" * 60)
with RadiodControl("radiod.local") as control:
ssrc = 14074000
# Create channel
print(f"Creating channel SSRC={ssrc}")
control.create_channel(
ssrc=ssrc,
frequency_hz=14.074e6,
preset="usb",
sample_rate=12000
)
# Use channel
print("Using channel for 2 seconds...")
time.sleep(2)
# IMPORTANT: Remove channel when done
print(f"Removing channel SSRC={ssrc}")
control.remove_channel(ssrc=ssrc)
print("✓ Channel removed, connection closed\n")
def pattern2_try_finally():
"""
Pattern 2: Try/Finally Block
Ensures cleanup even if exceptions occur.
"""
print("=" * 60)
print("Pattern 2: Try/Finally Block")
print("=" * 60)
control = RadiodControl("radiod.local")
ssrc = 7074000
try:
# Create channel
print(f"Creating channel SSRC={ssrc}")
control.create_channel(
ssrc=ssrc,
frequency_hz=7.074e6,
preset="usb"
)
# Use channel
print("Using channel for 2 seconds...")
time.sleep(2)
finally:
# ALWAYS runs, even if exception occurs
print(f"Removing channel SSRC={ssrc}")
control.remove_channel(ssrc=ssrc)
control.close()
print("✓ Channel removed, connection closed\n")
def pattern3_multiple_channels():
"""
Pattern 3: Managing Multiple Channels
Track all created channels and clean them up at the end.
"""
print("=" * 60)
print("Pattern 3: Multiple Channels with Cleanup")
print("=" * 60)
control = RadiodControl("radiod.local")
created_channels = []
try:
# Create multiple channels
frequencies = [14.074e6, 7.074e6, 3.573e6]
for freq in frequencies:
ssrc = int(freq)
print(f"Creating channel SSRC={ssrc}, freq={freq/1e6:.3f} MHz")
control.create_channel(
ssrc=ssrc,
frequency_hz=freq,
preset="usb"
)
created_channels.append(ssrc)
print(f"Using {len(created_channels)} channels for 2 seconds...")
time.sleep(2)
finally:
# Remove all created channels
print("Cleaning up channels...")
for ssrc in created_channels:
print(f" Removing SSRC={ssrc}")
control.remove_channel(ssrc=ssrc)
control.close()
print("✓ All channels removed, connection closed\n")
def pattern4_long_running_app():
"""
Pattern 4: Long-Running Application
For applications that create and destroy channels dynamically,
remove each channel immediately when done with it.
"""
print("=" * 60)
print("Pattern 4: Long-Running Application (Dynamic Channels)")
print("=" * 60)
with RadiodControl("radiod.local") as control:
# Simulate scanning multiple frequencies
frequencies = [14.074e6, 14.076e6, 14.078e6]
for freq in frequencies:
ssrc = int(freq)
# Create channel
print(f"Scanning {freq/1e6:.3f} MHz...")
control.create_channel(
ssrc=ssrc,
frequency_hz=freq,
preset="usb"
)
# Use it briefly
time.sleep(1)
# Remove immediately when done (don't wait until exit)
print(f" Done, removing channel")
control.remove_channel(ssrc=ssrc)
print("✓ All temporary channels removed as we went\n")
def anti_pattern_no_cleanup():
"""
ANTI-PATTERN: Don't do this!
Not removing channels causes them to accumulate in radiod.
Over time, this wastes resources and can cause issues.
"""
print("=" * 60)
print("⚠️ ANTI-PATTERN: No Cleanup (DON'T DO THIS!)")
print("=" * 60)
with RadiodControl("radiod.local") as control:
ssrc = 99999999
print(f"Creating channel SSRC={ssrc}")
control.create_channel(
ssrc=ssrc,
frequency_hz=14.074e6,
preset="usb"
)
print("Using channel...")
time.sleep(1)
# BAD: Not removing channel!
print("⚠️ Exiting without removing channel - this orphans the channel in radiod!")
print("❌ Channel left orphaned in radiod (bad!)\n")
def main():
"""Run all cleanup pattern examples"""
print("\n" + "=" * 60)
print("KA9Q-PYTHON: Channel Cleanup Best Practices")
print("=" * 60)
print()
print("This example shows proper patterns for removing channels")
print("to prevent radiod from accumulating unused channel instances.")
print()
try:
# Good patterns
pattern1_context_manager()
pattern2_try_finally()
pattern3_multiple_channels()
pattern4_long_running_app()
# Show anti-pattern (but clean it up)
anti_pattern_no_cleanup()
# Clean up the anti-pattern example
print("Cleaning up anti-pattern channel...")
with RadiodControl("radiod.local") as control:
control.remove_channel(ssrc=99999999)
print("✓ Orphaned channel cleaned up\n")
except Exception as e:
print(f"\n❌ Error: {e}")
print("Make sure radiod is running and accessible")
return 1
print("=" * 60)
print("Summary:")
print("=" * 60)
print("✓ Always call remove_channel() when done")
print("✓ Use try/finally or context managers for safety")
print("✓ In long-running apps, remove channels promptly")
print("✓ Track created channels and clean them all up")
print("❌ Don't leave orphaned channels in radiod")
print("=" * 60)
return 0
if __name__ == "__main__":
sys.exit(main())