How to use the imjoy.workers.worker_utils.Promise 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 / worker_template.py View on Github external
resolve.__jailed_pairs__ = reject
                    reject.__jailed_pairs__ = resolve
                    self.emit(
                        {
                            "type": "callback",
                            "id": id_,
                            "num": arg_num,
                            # 'pid'  : self.id,
                            "args": self._wrap(arguments),
                            "promise": self._wrap([resolve, reject]),
                        }
                    )

                if PYTHON3:
                    return FuturePromise(pfunc, self.client.loop)
                return Promise(pfunc)
github oeway / ImJoy-Engine / imjoy / workers / worker_template.py View on Github external
def pfunc(resolve, reject):
                resolve.__jailed_pairs__ = reject
                reject.__jailed_pairs__ = resolve
                call_func = {
                    "type": "method",
                    "name": name,
                    "pid": plugin_id,
                    "args": self._wrap(arguments),
                    "promise": self._wrap([resolve, reject]),
                }
                self.emit(call_func)

            if PYTHON3:
                return FuturePromise(pfunc, self.client.loop)
            return Promise(pfunc)
github oeway / ImJoy-Engine / imjoy / workers / worker_utils3.py View on Github external
def __init__(self, pfunc, loop):
        """Set up promise."""
        self.loop = loop
        Promise.__init__(self, pfunc)
        asyncio.Future.__init__(self)
github oeway / ImJoy-Engine / imjoy / workers / worker_utils3.py View on Github external
import asyncio

from imjoy.workers.worker_utils import Promise


def make_coro(func):
    """Wrap a normal function with a coroutine."""

    async def wrapper(*args, **kwargs):
        """Run the normal function."""
        return func(*args, **kwargs)

    return wrapper


class FuturePromise(Promise, asyncio.Future):
    """Represent a promise as a future."""

    def __init__(self, pfunc, loop):
        """Set up promise."""
        self.loop = loop
        Promise.__init__(self, pfunc)
        asyncio.Future.__init__(self)

    def resolve(self, result):
        """Resolve promise."""
        if self._resolve_handler or self._finally_handler:
            super().resolve(result)
        else:
            self.loop.call_soon(self.set_result, result)

    def reject(self, error):