Skip to content

Commit c33fafd

Browse files
committed
Create one longlived asyncio event loop
1 parent d3ce243 commit c33fafd

File tree

1 file changed

+16
-12
lines changed

1 file changed

+16
-12
lines changed

mycodo/inputs/openhydroponics.py

Lines changed: 16 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
# coding=utf-8
2-
import asyncio
32
import copy
43

54
from mycodo.inputs.base_input import AbstractInput
65
from mycodo.databases.models import DeviceMeasurements
76
from mycodo.utils.asyncio import AsyncLoop
87

9-
node_manager = None
8+
loop = AsyncLoop()
9+
loop.initialize()
1010

1111
measurements_dict = {
1212
}
@@ -54,10 +54,12 @@ async def execute_at_modification_async(
5454
custom_options_channels_dict_postsave,
5555
)
5656

57+
node_manager = NodeManager()
5758
await node_manager.init()
5859
node = await node_manager.request_node(device_id)
5960
if not node:
6061
messages["error"].append(f"Node {mod_input.device_id} not found")
62+
await node_manager.deinit()
6163
return (
6264
messages,
6365
mod_input,
@@ -103,6 +105,7 @@ async def execute_at_modification_async(
103105
new_measurement.channel = endpoint.endpoint_id
104106
new_measurement.save()
105107

108+
await node_manager.deinit()
106109
return (
107110
messages,
108111
mod_input,
@@ -121,7 +124,7 @@ def execute_at_modification(
121124
custom_options_channels_dict_postsave,
122125
):
123126

124-
return asyncio.run(
127+
return loop.call_async(
125128
execute_at_modification_async(
126129
messages,
127130
mod_input,
@@ -169,16 +172,18 @@ def execute_at_modification(
169172

170173

171174
async def populate_nodes():
175+
node_manager = NodeManager()
176+
await node_manager.init()
172177
async for node in node_manager:
173178
INPUT_INFORMATION["custom_options"][0]["options_select"].append(
174179
(str(node.uuid), f"Node: {node.uuid}")
175180
)
181+
await node_manager.deinit()
176182

177183

178184
try:
179185
from openhydroponics.dbus import NodeManager
180-
node_manager = NodeManager()
181-
asyncio.run(populate_nodes())
186+
loop.call_async(populate_nodes())
182187
except ImportError:
183188
# Before the dependencies are installed, this will raise an ImportError
184189
# Just ignore it for now
@@ -195,7 +200,7 @@ def __init__(self, input_dev, testing=False):
195200
self.device_id = None
196201
self.options_channels = None
197202

198-
self.loop = AsyncLoop()
203+
self.node_manager = NodeManager()
199204

200205
# Set custom option variables to defaults or user-set values
201206
self.setup_custom_options(INPUT_INFORMATION["custom_options"], input_dev)
@@ -204,7 +209,7 @@ def __init__(self, input_dev, testing=False):
204209
self.try_initialize()
205210

206211
async def async_initialize(self):
207-
await node_manager.init()
212+
await self.node_manager.init()
208213

209214
self.measurement_info = {}
210215
for measurement in self.device_measurements.all():
@@ -215,14 +220,13 @@ async def async_initialize(self):
215220
] = measurement.measurement
216221

217222
def initialize(self):
218-
self.loop.initialize()
219-
self.loop.call_async(self.async_initialize())
223+
loop.call_async(self.async_initialize())
220224

221225
async def get_measurement_async(self):
222226
if not self.device_id:
223227
self.logger.error("Device ID not set")
224228
return None
225-
node = await node_manager.request_node(self.device_id)
229+
node = await self.node_manager.request_node(self.device_id)
226230
if not node:
227231
self.logger.error(f"Node {self.device_id} not found")
228232
return None
@@ -236,7 +240,7 @@ async def get_measurement_async(self):
236240
return self.return_dict
237241

238242
def get_measurement(self):
239-
return self.loop.call_async(self.get_measurement_async())
243+
return loop.call_async(self.get_measurement_async())
240244

241245
def stop_input(self):
242-
self.loop.stop()
246+
loop.call_async(self.node_manager.deinit())

0 commit comments

Comments
 (0)