How to use the imjoy.workers.python_client.BaseClient function in imjoy

To help you get started, we’ve selected a few imjoy examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github oeway / ImJoy-Engine / imjoy / workers / python_client.py View on Github external
def on_disconnect():
            if not conn.opt.daemon:
                conn.exit(1)

        conn.emit = emit
        self.sio.on("disconnect", on_disconnect)
        self.sio.on("to_plugin_" + conn.secret, sio_plugin_message)
        emit({"type": "initialized", "dedicatedThread": True})
        logger.info("Plugin %s initialized", conn.opt.id)

    def run_forever(self, conn):
        """Run forever."""
        raise NotImplementedError


class Client(BaseClient):
    """Represent a sync socketio client."""

    def __init__(self):
        """Set up client instance."""
        super(Client, self).__init__()
        self.queue = queue.Queue()
        self.task_worker = task_worker

    def run_forever(self, conn):
        """Run forever."""
        self.task_worker(conn, self.queue, logger, conn.abort)
github oeway / ImJoy-Engine / imjoy / workers / python3_client.py View on Github external
if method is None:
                raise Exception(
                    "Callback function can only called once, "
                    "if you want to call a function for multiple times, "
                    "please make it as a plugin api function. "
                    "See https://imjoy.io/docs for more details."
                )
            args = conn.unwrap(job["args"], True)
            result = method(*args)
            if result is not None and inspect.isawaitable(result):
                await result
        except Exception:  # pylint: disable=broad-except
            logger.error("Error in method %s: %s", job["num"], traceback.format_exc())


class AsyncClient(BaseClient):
    """Represent an async socketio client."""

    # pylint: disable=too-few-public-methods

    def __init__(self, conn, opt):
        """Set up client instance."""
        super().__init__(conn, opt)
        self.loop = asyncio.get_event_loop()
        self.janus_queue = janus.Queue(loop=self.loop)
        self.queue = self.janus_queue.sync_q

    def run_forever(self):
        """Run forever."""
        workers = [
            task_worker(self.conn, self.janus_queue.async_q, logger, self.conn.abort)
            for i in range(10)
github oeway / ImJoy-Engine / imjoy / workers / python_client.py View on Github external
def __init__(self, id=None):
        """Set up client instance."""
        self.id = id or str(uuid.uuid4())
        self.sio = socketio.Client()
        BaseClient._clients[self.id] = self
github oeway / ImJoy-Engine / imjoy / workers / python3_client.py View on Github external
if method is None:
                raise Exception(
                    "Callback function can only called once, "
                    "if you want to call a function for multiple times, "
                    "please make it as a plugin api function. "
                    "See https://imjoy.io/docs for more details."
                )
            args = conn.unwrap(job["args"], True)
            result = method(*args)
            if result is not None and inspect.isawaitable(result):
                await result
        except Exception:  # pylint: disable=broad-except
            logger.error("Error in method %s: %s", job["num"], traceback.format_exc())


class AsyncClient(BaseClient):
    """Represent an async socketio client."""

    # pylint: disable=too-few-public-methods

    def __init__(self, id=None):
        """Set up client instance."""
        super().__init__(id)
        self.loop = asyncio.get_event_loop()
        self.janus_queue = janus.Queue(loop=self.loop)
        self.queue = self.janus_queue.sync_q
        self.task_worker = task_worker

    def run_forever(self, conn):
        """Run forever."""

        if self.loop.is_running():
github oeway / ImJoy-Engine / imjoy / workers / python_worker.py View on Github external
def add_plugin(plugin_id, client_id):
        opt = dotdict(id=plugin_id, secret="", work_dir="")
        client = BaseClient.get_client(client_id)
        p = PluginConnection(client, opt)
        return p