From ed6badb08855e124e8848dc28df7f27a164d1526 Mon Sep 17 00:00:00 2001 From: Daniil Fajnberg Date: Fri, 25 Feb 2022 19:09:28 +0100 Subject: [PATCH] moved imports for unix socket connections to init methods of client and server --- src/asyncio_taskpool/client.py | 6 ++++-- src/asyncio_taskpool/server.py | 6 ++++-- 2 files changed, 8 insertions(+), 4 deletions(-) diff --git a/src/asyncio_taskpool/client.py b/src/asyncio_taskpool/client.py index bb1a28f..eab4c37 100644 --- a/src/asyncio_taskpool/client.py +++ b/src/asyncio_taskpool/client.py @@ -23,7 +23,7 @@ import json import shutil import sys from abc import ABC, abstractmethod -from asyncio.streams import StreamReader, StreamWriter, open_unix_connection +from asyncio.streams import StreamReader, StreamWriter from pathlib import Path from typing import Optional @@ -153,6 +153,8 @@ class UnixControlClient(ControlClient): The `_socket_path` attribute is set to the `Path` object created from the `socket_path` argument. """ + from asyncio.streams import open_unix_connection + self._open_unix_connection = open_unix_connection self._socket_path = Path(socket_path) super().__init__(**conn_kwargs) @@ -164,7 +166,7 @@ class UnixControlClient(ControlClient): otherwise, the stream-reader and -writer tuple is returned. """ try: - return await open_unix_connection(self._socket_path, **kwargs) + return await self._open_unix_connection(self._socket_path, **kwargs) except FileNotFoundError: print("No socket at", self._socket_path, file=sys.stderr) return None, None diff --git a/src/asyncio_taskpool/server.py b/src/asyncio_taskpool/server.py index e0b9005..d7a0f4c 100644 --- a/src/asyncio_taskpool/server.py +++ b/src/asyncio_taskpool/server.py @@ -23,7 +23,7 @@ import logging from abc import ABC, abstractmethod from asyncio import AbstractServer from asyncio.exceptions import CancelledError -from asyncio.streams import StreamReader, StreamWriter, start_unix_server +from asyncio.streams import StreamReader, StreamWriter from asyncio.tasks import Task, create_task from pathlib import Path from typing import Optional, Union @@ -137,11 +137,13 @@ class UnixControlServer(ControlServer): _client_class = UnixControlClient def __init__(self, pool: SimpleTaskPool, **server_kwargs) -> None: + from asyncio.streams import start_unix_server + self._start_unix_server = start_unix_server self._socket_path = Path(server_kwargs.pop('path')) super().__init__(pool, **server_kwargs) async def _get_server_instance(self, client_connected_cb: ConnectedCallbackT, **kwargs) -> AbstractServer: - server = await start_unix_server(client_connected_cb, self._socket_path, **kwargs) + server = await self._start_unix_server(client_connected_cb, self._socket_path, **kwargs) log.debug("Opened socket '%s'", str(self._socket_path)) return server