Skip to content

Commit

Permalink
Reorder methods of Client
Browse files Browse the repository at this point in the history
  • Loading branch information
xjules committed Dec 16, 2024
1 parent 518de38 commit d7548d0
Showing 1 changed file with 26 additions and 26 deletions.
52 changes: 26 additions & 26 deletions src/_ert/forward_model_runner/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,32 +29,6 @@ class Client:
DEFAULT_ACK_TIMEOUT = 5
_receiver_task: asyncio.Task[None] | None

async def __aenter__(self) -> Self:
await self.connect()
return self

async def __aexit__(
self, exc_type: Any, exc_value: Any, exc_traceback: Any
) -> None:
try:
await self.send(DISCONNECT_MSG)
except ClientConnectionError:
logger.error("No ack for dealer disconnection. Connection is down!")
finally:
self.socket.disconnect(self.url)
await self._term_receiver_task()
self.term()

def term(self) -> None:
self.socket.close()
self.context.term()

async def _term_receiver_task(self) -> None:
if self._receiver_task and not self._receiver_task.done():
self._receiver_task.cancel()
await asyncio.gather(self._receiver_task, return_exceptions=True)
self._receiver_task = None

def __init__(
self,
url: str,
Expand Down Expand Up @@ -83,6 +57,32 @@ def __init__(

self._receiver_task = None

async def __aenter__(self) -> Self:
await self.connect()
return self

async def __aexit__(
self, exc_type: Any, exc_value: Any, exc_traceback: Any
) -> None:
try:
await self.send(DISCONNECT_MSG)
except ClientConnectionError:
logger.error("No ack for dealer disconnection. Connection is down!")
finally:
self.socket.disconnect(self.url)
await self._term_receiver_task()
self.term()

def term(self) -> None:
self.socket.close()
self.context.term()

async def _term_receiver_task(self) -> None:
if self._receiver_task and not self._receiver_task.done():
self._receiver_task.cancel()
await asyncio.gather(self._receiver_task, return_exceptions=True)
self._receiver_task = None

async def connect(self) -> None:
self.socket.connect(self.url)
await self._term_receiver_task()
Expand Down

0 comments on commit d7548d0

Please sign in to comment.