Skip to content

Commit

Permalink
Get rid of loops
Browse files Browse the repository at this point in the history
  • Loading branch information
yozik04 committed Sep 17, 2024
1 parent 3651314 commit f1ba7f7
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 18 deletions.
10 changes: 6 additions & 4 deletions paradox/connections/ip/connection.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ def __init__(self, host="127.0.0.1", port=10000):
self.port = port

async def _try_connect(self):
_, self._protocol = await self.loop.create_connection(
_, self._protocol = await asyncio.get_event_loop().create_connection(
self._make_protocol, host=self.host, port=self.port
)

Expand Down Expand Up @@ -90,7 +90,9 @@ def set_key(self, value):
self._protocol.key = value

def on_ip_message(self, container: Container):
return self.loop.create_task(self.ip_handler_registry.handle(container))
return asyncio.get_event_loop().create_task(
self.ip_handler_registry.handle(container)
)

async def wait_for_ip_message(self, timeout=cfg.IO_TIMEOUT) -> Container:
future = FutureHandler()
Expand All @@ -115,7 +117,7 @@ def __init__(
self.port = port

async def _try_connect(self) -> None:
_, self._protocol = await self.loop.create_connection(
_, self._protocol = await asyncio.get_event_loop().create_connection(
self._make_protocol, host=self.host, port=self.port
)

Expand Down Expand Up @@ -146,7 +148,7 @@ def write(self, data: bytes):

async def _try_connect(self) -> None:
await self.stun_session.connect()
_, self._protocol = await self.loop.create_connection(
_, self._protocol = await asyncio.get_event_loop().create_connection(
self._make_protocol, sock=self.stun_session.get_socket()
)

Expand Down
15 changes: 6 additions & 9 deletions paradox/connections/serial_connection.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,10 @@
# -*- coding: utf-8 -*-


import asyncio
import logging
import os
import stat
import typing

import serial_asyncio
from serial import SerialException
import serial_asyncio

from ..exceptions import SerialConnectionOpenFailed
from .connection import Connection
Expand Down Expand Up @@ -67,12 +64,12 @@ async def connect(self) -> bool:
logger.error(f"Failed to update file {self.port_path} permissions")
return False

self.connected_future = self.loop.create_future()
open_timeout_handler = self.loop.call_later(5, self.open_timeout)
self.connected_future = asyncio.get_event_loop().create_future()
open_timeout_handler = asyncio.get_event_loop().call_later(5, self.open_timeout)

try:
_, self._protocol = await serial_asyncio.create_serial_connection(
self.loop, self.make_protocol, self.port_path, self.baud
asyncio.get_event_loop(), self.make_protocol, self.port_path, self.baud
)

return await self.connected_future
Expand All @@ -81,7 +78,7 @@ async def connect(self) -> bool:
raise SerialConnectionOpenFailed(
"Connection to serial port failed"
) from e # PAICriticalException
except:
except Exception:
logger.exception("Unable to connect to Serial")
finally:
open_timeout_handler.cancel()
Expand Down
10 changes: 5 additions & 5 deletions paradox/interfaces/text/signal.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,12 @@ def __init__(self, alarm):
)

self.signal = None
self.loop = None
self.glib_loop = None

def stop(self):
"""Stops the Signal Interface Thread"""
if self.loop is not None:
self.loop.quit()
if self.glib_loop is not None:
self.glib_loop.quit()

super().stop()

Expand All @@ -46,11 +46,11 @@ async def run(self):

self.signal = bus.get("org.asamk.Signal")
self.signal.onMessageReceived = self.handle_message
self.loop = GLib.MainLoop()
self.glib_loop = GLib.MainLoop()

logger.debug("Signal Interface Running")

asyncio.get_event_loop().run_in_executor(None, self.loop.run)
asyncio.get_event_loop().run_in_executor(None, self.glib_loop.run)

def send_message(self, message: str, level: EventLevel):
if self.signal is None:
Expand Down

0 comments on commit f1ba7f7

Please sign in to comment.