From c105949155fca6219f35b9fafbd861b288f2b4ad Mon Sep 17 00:00:00 2001 From: Robert Jordens Date: Tue, 2 Feb 2016 15:14:24 -0700 Subject: [PATCH] pc_rpc.Client: support socket timeouts ... and fix two flake8 errors. --- artiq/protocols/pc_rpc.py | 15 ++++++++++----- 1 file changed, 10 insertions(+), 5 deletions(-) diff --git a/artiq/protocols/pc_rpc.py b/artiq/protocols/pc_rpc.py index d1de343a9..a861b48cd 100644 --- a/artiq/protocols/pc_rpc.py +++ b/artiq/protocols/pc_rpc.py @@ -57,8 +57,7 @@ def _validate_target_name(target_name, target_names): target_name = target_names[0] elif target_name not in target_names: raise IncompatibleServer( - "valid target name(s): " + - " ".join(sorted(target_names))) + "valid target name(s): " + " ".join(sorted(target_names))) return target_name @@ -92,9 +91,14 @@ class Client: Use ``None`` to skip selecting a target. The list of targets can then be retrieved using ``get_rpc_id`` and then one can be selected later using ``select_rpc_target``. + :param timeout: Socket operation timeout. Use ``None`` for blocking + (default), ``0`` for non-blocking, and a finite value to raise + ``socket.timeout`` if an operation does not complete within the + given time. See also ``socket.create_connection()`` and + ``socket.settimeout()`` in the Python standard library. """ - def __init__(self, host, port, target_name=AutoTarget): - self.__socket = socket.create_connection((host, port)) + def __init__(self, host, port, target_name=AutoTarget, timeout=None): + self.__socket = socket.create_connection((host, port), timeout) try: self.__socket.sendall(_init_string) @@ -485,7 +489,8 @@ class Server(_AsyncioServer): obj = {"status": "ok", "ret": doc} elif obj["action"] == "call": logger.debug("calling %s", _PrettyPrintCall(obj)) - if self.builtin_terminate and obj["name"] == "terminate": + if (self.builtin_terminate and obj["name"] == + "terminate"): self._terminate_request.set() obj = {"status": "ok", "ret": None} else: