Skip to content

Commit 9ad28c3

Browse files
remote/coordinator: store event loop in loop attribute
Instead of using asyncio.get_event_loop() in various places in the Coordinator class, store it in an attribute called loop. Signed-off-by: Bastian Krause <[email protected]>
1 parent f32d418 commit 9ad28c3

File tree

1 file changed

+7
-8
lines changed

1 file changed

+7
-8
lines changed

labgrid/remote/coordinator.py

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -194,7 +194,8 @@ def __init__(self) -> None:
194194
self.clients: dict[str, ClientSession] = {}
195195
self.load()
196196

197-
self.poll_task = asyncio.get_event_loop().create_task(self.poll())
197+
self.loop = asyncio.get_event_loop()
198+
self.poll_task = self.loop.create_task(self.poll())
198199

199200
async def _poll_step(self):
200201
# save changes
@@ -216,8 +217,7 @@ async def _poll_step(self):
216217
traceback.print_exc()
217218

218219
async def poll(self):
219-
loop = asyncio.get_event_loop()
220-
while not loop.is_closed():
220+
while not self.loop.is_closed():
221221
try:
222222
await asyncio.sleep(15.0)
223223
await self._poll_step()
@@ -247,11 +247,10 @@ async def save(self):
247247
places = yaml.dump(places)
248248
places = places.encode()
249249

250-
loop = asyncio.get_event_loop()
251250
logging.debug("Awaiting resources")
252-
await loop.run_in_executor(None, atomic_replace, "resources.yaml", resources)
251+
await self.loop.run_in_executor(None, atomic_replace, "resources.yaml", resources)
253252
logging.debug("Awaiting places")
254-
await loop.run_in_executor(None, atomic_replace, "places.yaml", places)
253+
await self.loop.run_in_executor(None, atomic_replace, "places.yaml", places)
255254

256255
def load(self):
257256
try:
@@ -310,7 +309,7 @@ async def request_task():
310309
except Exception:
311310
logging.exception("error in client message handler")
312311

313-
runnning_request_task = asyncio.get_event_loop().create_task(request_task())
312+
runnning_request_task = self.loop.create_task(request_task())
314313

315314
try:
316315
async for out_msg in queue_as_aiter(out_msg_queue):
@@ -406,7 +405,7 @@ async def request_task():
406405
except Exception:
407406
logging.exception("error in exporter message handler")
408407

409-
runnning_request_task = asyncio.get_event_loop().create_task(request_task())
408+
runnning_request_task = self.loop.create_task(request_task())
410409

411410
try:
412411
async for cmd in queue_as_aiter(command_queue):

0 commit comments

Comments
 (0)