Python typing.Coroutine() Examples

The following are 30 code examples of typing.Coroutine(). You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may also want to check out all available functions/classes of the module typing , or try the search function .
Example #1
Source File: utils.py    From quart with MIT License 8 votes vote down vote up
def run_sync(func: Callable[..., Any]) -> Callable[..., Coroutine[Any, None, None]]:
    """Ensure that the sync function is run within the event loop.

    If the *func* is not a coroutine it will be wrapped such that
    it runs in the default executor (use loop.set_default_executor
    to change). This ensures that synchronous functions do not
    block the event loop.
    """

    @wraps(func)
    async def _wrapper(*args: Any, **kwargs: Any) -> Any:
        loop = asyncio.get_running_loop()
        result = await loop.run_in_executor(
            None, copy_context().run, partial(func, *args, **kwargs)
        )
        if isgenerator(result):
            return run_sync_iterable(result)  # type: ignore
        else:
            return result

    _wrapper._quart_async_wrapper = True  # type: ignore
    return _wrapper 
Example #2
Source File: server.py    From py-stellar-base with Apache License 2.0 7 votes vote down vote up
def load_account(
        self, account_id: Union[Keypair, str]
    ) -> Union[Account, Coroutine[Any, Any, Account]]:
        """Fetches an account's most current state in the ledger and then creates
        and returns an :class:`stellar_sdk.account.Account` object.

        :param account_id: The account to load.
        :return: an :class:`stellar_sdk.account.Account` object.
        :raises:
            :exc:`ConnectionError <stellar_sdk.exceptions.ConnectionError>`
            :exc:`NotFoundError <stellar_sdk.exceptions.NotFoundError>`
            :exc:`BadRequestError <stellar_sdk.exceptions.BadRequestError>`
            :exc:`BadResponseError <stellar_sdk.exceptions.BadResponseError>`
            :exc:`UnknownRequestError <stellar_sdk.exceptions.UnknownRequestError>`
        """

        if isinstance(account_id, Keypair):
            account = account_id.public_key
        else:
            account = account_id
        if self.__async:
            return self.__load_account_async(account)
        return self.__load_account_sync(account) 
Example #3
Source File: bridge.py    From mautrix-telegram with GNU Affero General Public License v3.0 7 votes vote down vote up
def cleanup_old_portal_while_bridging(evt: CommandEvent, portal: "po.Portal"
                                            ) -> Tuple[
    bool, Optional[Coroutine[None, None, None]]]:
    if not portal.mxid:
        await evt.reply("The portal seems to have lost its Matrix room between you"
                        "calling `$cmdprefix+sp bridge` and this command.\n\n"
                        "Continuing without touching previous Matrix room...")
        return True, None
    elif evt.args[0] == "delete-and-continue":
        return True, portal.cleanup_portal("Portal deleted (moving to another room)")
    elif evt.args[0] == "unbridge-and-continue":
        return True, portal.cleanup_portal("Room unbridged (portal moving to another room)",
                                           puppets_only=True)
    else:
        await evt.reply(
            "The chat you were trying to bridge already has a Matrix portal room.\n\n"
            "Please use `$cmdprefix+sp delete-and-continue` or `$cmdprefix+sp unbridge-and-"
            "continue` to either delete or unbridge the existing room (respectively) and "
            "continue with the bridging.\n\n"
            "If you changed your mind, use `$cmdprefix+sp cancel` to cancel.")
        return False, None 
Example #4
Source File: default.py    From tartiflette with MIT License 6 votes vote down vote up
def sync_arguments_coercer(
    *coroutines: List[Coroutine],
) -> List[Union[Any, Exception]]:
    """
    Coerce arguments synchronously.

    :param coroutines: list of coroutine to await
    :type coroutines: List[Coroutine]
    :return: the result of coroutines
    :rtype: List[Union[Any, Exception]]
    """
    results = []
    for coroutine in coroutines:
        try:
            result = await coroutine
        except Exception as e:  # pylint: disable=broad-except
            result = e
        results.append(result)
    return results 
Example #5
Source File: utils.py    From gita with MIT License 6 votes vote down vote up
def exec_async_tasks(tasks: List[Coroutine]) -> List[Union[None, str]]:
    """
    Execute tasks asynchronously
    """
    # TODO: asyncio API is nicer in python 3.7
    if platform.system() == 'Windows':
        loop = asyncio.ProactorEventLoop()
        asyncio.set_event_loop(loop)
    else:
        loop = asyncio.get_event_loop()

    try:
        errors = loop.run_until_complete(asyncio.gather(*tasks))
    finally:
        loop.close()
    return errors 
Example #6
Source File: manager.py    From python-sdk with MIT License 6 votes vote down vote up
def _dispatch_internal(
            self, actor_id: ActorId, method_context: ActorMethodContext,
            dispatch_action: Callable[[Actor], Coroutine[Any, Any, Optional[bytes]]]) -> object:
        # Activate actor when actor is invoked.
        if actor_id.id not in self._active_actors:
            await self.activate_actor(actor_id)
        actor = None
        async with self._active_actors_lock:
            actor = self._active_actors.get(actor_id.id, None)
        if not actor:
            raise ValueError(f'{actor_id} is not activated')

        try:
            await actor._on_pre_actor_method_internal(method_context)
            retval = await dispatch_action(actor)
            await actor._on_post_actor_method_internal(method_context)
        except DaprInternalError as ex:
            await actor._on_invoke_failed_internal(ex)
            raise ex

        return retval 
Example #7
Source File: library.py    From galaxy-integration-humblebundle with GNU General Public License v3.0 6 votes vote down vote up
def __gather_no_exceptions(tasks: Iterable[Coroutine]):
        """Wrapper around asyncio.gather(*args, return_exception=True)
        Returns list of non-exception items. If every item is exception, raise first of them, else logs them.
        Use case: https://github.com/UncleGoogle/galaxy-integration-humblebundle/issues/59
        """
        items = await asyncio.gather(*tasks, return_exceptions=True)
        if len(items) == 0:
            return []

        err: List[Exception] = []
        ok: List[Any] = []
        for it in items:
            (err if isinstance(it, Exception) else ok).append(it)

        if len(ok) == 0:
            raise err[0]
        if err and len(err) != len(items):
            logger.error(f'Exception(s) occured: [{err}].\nSkipping and going forward')
        return ok 
Example #8
Source File: utils.py    From async-worker with MIT License 6 votes vote down vote up
def __call__(
        self, coro: Optional[Callable[..., Coroutine]] = None, name: str = None
    ) -> Union[Callable[..., Coroutine], "Timeit"]:
        if name:
            child = Timeit(name=name)
            child._transactions = self._transactions
            return child

        if not coro:
            raise ValueError(
                "Invalid method call. " '"coro" or "name" must be provided'
            )

        @wraps(coro)
        async def wrapped(*args, **kwargs):
            async with self:
                return await coro(*args, **kwargs)

        return wrapped 
Example #9
Source File: task_queue.py    From aries-cloudagent-python with Apache License 2.0 6 votes vote down vote up
def put(
        self, coro: Coroutine, task_complete: Callable = None, ident: str = None
    ) -> PendingTask:
        """
        Add a new task to the queue, delaying execution if busy.

        Args:
            coro: The coroutine to run
            task_complete: A callback to run on completion
            ident: A string identifier for the task

        Returns: a future resolving to the asyncio task instance once queued

        """
        pending = PendingTask(coro, task_complete, ident)
        if self._cancelled:
            pending.cancel()
        elif self.ready:
            pending.task = self.run(coro, task_complete, pending.ident)
        else:
            self.add_pending(pending)
        return pending 
Example #10
Source File: manager.py    From aries-cloudagent-python with Apache License 2.0 6 votes vote down vote up
def __init__(
        self,
        context: InjectionContext,
        receive_inbound: Coroutine,
        return_inbound: Callable = None,
    ):
        """Initialize an `InboundTransportManager` instance."""
        self.context = context
        self.max_message_size = 0
        self.receive_inbound = receive_inbound
        self.return_inbound = return_inbound
        self.registered_transports = {}
        self.running_transports = {}
        self.sessions = OrderedDict()
        self.session_limit: asyncio.Semaphore = None
        self.task_queue = TaskQueue()
        self.undelivered_queue: DeliveryQueue = None 
Example #11
Source File: dispatcher.py    From aries-cloudagent-python with Apache License 2.0 6 votes vote down vote up
def queue_message(
        self,
        inbound_message: InboundMessage,
        send_outbound: Coroutine,
        send_webhook: Coroutine = None,
        complete: Callable = None,
    ) -> PendingTask:
        """
        Add a message to the processing queue for handling.

        Args:
            inbound_message: The inbound message instance
            send_outbound: Async function to send outbound messages
            send_webhook: Async function to dispatch a webhook
            complete: Function to call when the handler has completed

        Returns:
            A pending task instance resolving to the handler task

        """
        return self.put_task(
            self.handle_message(inbound_message, send_outbound, send_webhook), complete
        ) 
Example #12
Source File: dispatcher.py    From aries-cloudagent-python with Apache License 2.0 6 votes vote down vote up
def __init__(
        self,
        context: RequestContext,
        inbound_message: InboundMessage,
        send_outbound: Coroutine,
        send_webhook: Coroutine = None,
        **kwargs,
    ):
        """
        Initialize an instance of `DispatcherResponder`.

        Args:
            context: The request context of the incoming message
            inbound_message: The inbound message triggering this handler
            send_outbound: Async function to send outbound message
            send_webhook: Async function to dispatch a webhook

        """
        super().__init__(**kwargs)
        self._context = context
        self._inbound_message = inbound_message
        self._send = send_outbound
        self._webhook = send_webhook 
Example #13
Source File: async_tools.py    From lightbus with Apache License 2.0 6 votes vote down vote up
def block(coroutine: Coroutine, loop=None, *, timeout=None):
    """Call asynchronous code synchronously

    Note that this cannot be used inside an event loop.
    """
    loop = loop or get_event_loop()
    if loop.is_running():
        if hasattr(coroutine, "close"):
            coroutine.close()
        raise CannotBlockHere(
            "It appears you have tried to use a blocking API method "
            "from within an event loop. Unfortunately this is unsupported. "
            "Instead, use the async version of the method."
        )
    try:
        if timeout is None:
            val = loop.run_until_complete(coroutine)
        else:
            val = loop.run_until_complete(asyncio.wait_for(coroutine, timeout=timeout))
    except Exception as e:
        # The intention here is to get sensible stack traces from exceptions within blocking calls
        raise e
    return val 
Example #14
Source File: bus_client.py    From lightbus with Apache License 2.0 6 votes vote down vote up
def add_background_task(self, coroutine: Union[Coroutine, asyncio.Future]):
        """Run a coroutine in the background

        The provided coroutine will be run in the background once
        Lightbus startup is complete.

        The coroutine will be cancelled when the bus client is closed.

        The Lightbus process will exit if the coroutine raises an exception.
        See lightbus.utilities.async_tools.check_for_exception() for details.
        """

        # Store coroutine for starting once the worker starts
        self._background_coroutines.append(coroutine)

    # Utilities 
Example #15
Source File: test_typing.py    From Project-New-Reign---Nemesis-Main with GNU General Public License v3.0 6 votes vote down vote up
def test_coroutine(self):
        ns = {}
        exec(
            "async def foo():\n"
            "    return\n",
            globals(), ns)
        foo = ns['foo']
        g = foo()
        self.assertIsInstance(g, typing.Coroutine)
        with self.assertRaises(TypeError):
            isinstance(g, typing.Coroutine[int])
        self.assertNotIsInstance(foo, typing.Coroutine)
        try:
            g.send(None)
        except StopIteration:
            pass 
Example #16
Source File: app.py    From async-worker with MIT License 5 votes vote down vote up
def run_on_startup(self, coro: Callable[["App"], Coroutine]) -> None:
        """
        Registers a coroutine to be awaited for during app startup
        """
        self._on_startup.append(coro) 
Example #17
Source File: diagnosis.py    From hummingbot with Apache License 2.0 5 votes vote down vote up
def get_wrapped_coroutine(t: asyncio.Task) -> Union[Coroutine, Generator]:
    if "safe_wrapper" in str(t):
        return t.get_coro().cr_frame.f_locals["c"]
    else:
        return t.get_coro() 
Example #18
Source File: diagnosis.py    From hummingbot with Apache License 2.0 5 votes vote down vote up
def get_coro_name(coro: Union[Coroutine, Generator]) -> str:
    if hasattr(coro, '__qualname__') and coro.__qualname__:
        coro_name = coro.__qualname__
    elif hasattr(coro, '__name__') and coro.__name__:
        coro_name = coro.__name__
    else:
        coro_name = f'<{type(coro).__name__} without __name__>'
    return f'{coro_name}()' 
Example #19
Source File: connection.py    From vibora with MIT License 5 votes vote down vote up
def read_exactly(self, length) -> Coroutine:
        """

        :param length:
        :return:
        """
        return self.reader.readexactly(length) 
Example #20
Source File: executors.py    From rclpy with Apache License 2.0 5 votes vote down vote up
def create_task(self, callback: Union[Callable, Coroutine], *args, **kwargs) -> Task:
        """
        Add a callback or coroutine to be executed during :meth:`spin` and return a Future.

        Arguments to this function are passed to the callback.

        :param callback: A callback to be run in the executor.
        """
        task = Task(callback, args, kwargs, executor=self)
        with self._tasks_lock:
            self._tasks.append((task, None, None))
            self._guard.trigger()
        # Task inherits from Future
        return task 
Example #21
Source File: executors.py    From rclpy with Apache License 2.0 5 votes vote down vote up
def await_or_execute(callback: Union[Callable, Coroutine], *args) -> Any:
    """Await a callback if it is a coroutine, else execute it."""
    if inspect.iscoroutinefunction(callback):
        # Await a coroutine
        return await callback(*args)
    else:
        # Call a normal function
        return callback(*args) 
Example #22
Source File: diagnosis.py    From hummingbot with Apache License 2.0 5 votes vote down vote up
def active_tasks() -> pd.DataFrame:
    tasks: List[asyncio.Task] = [t for t in asyncio.Task.all_tasks() if not t.done()]
    coroutines: List[Union[Coroutine, Generator]] = [get_wrapped_coroutine(t) for t in tasks]
    func_names: List[str] = [get_coro_name(c) for c in coroutines]
    retval: pd.DataFrame = pd.DataFrame([{"func_name": f, "coroutine": c, "task": t}
                                         for f, c, t in zip(func_names, coroutines, tasks)],
                                        columns=["func_name", "coroutine", "task"]).set_index("func_name")
    retval.sort_index(inplace=True)
    return retval 
Example #23
Source File: federation.py    From py-stellar-base with Apache License 2.0 5 votes vote down vote up
def resolve_account_id(
    account_id: str,
    domain: str = None,
    federation_url: str = None,
    client: Union[BaseAsyncClient, BaseSyncClient] = None,
    use_http: bool = False,
) -> Union[Coroutine[Any, Any, FederationRecord], FederationRecord]:
    """Given an account ID, get their federation record if the user was found

    :param account_id: Account ID (ex. GBYNR2QJXLBCBTRN44MRORCMI4YO7FZPFBCNOKTOBCAAFC7KC3LNPRYS)
    :param domain: Get ``federation_url`` from the domain, you don't need to set this value if ``federation_url`` is set.
    :param federation_url: The federation server URL (ex. https://stellar.org/federation).
    :param client: Http Client used to send the request.
    :param use_http: Specifies whether the request should go over plain HTTP vs HTTPS.
        Note it is recommend that you *always* use HTTPS.
    :return: Federation record.
    """
    if domain is None and federation_url is None:
        raise ValueError("You should provide either `domain` or `federation_url`.")

    if not client:
        client = RequestsClient()
    if isinstance(client, BaseAsyncClient):
        return __resolve_account_id_async(
            account_id, domain, federation_url, client, use_http
        )
    elif isinstance(client, BaseSyncClient):
        return __resolve_account_id_sync(
            account_id, domain, federation_url, client, use_http
        )
    else:
        raise TypeError(
            "This `client` class should be an instance "
            "of `stellar_sdk.client.base_async_client.BaseAsyncClient` "
            "or `stellar_sdk.client.base_sync_client.BaseSyncClient`."
        ) 
Example #24
Source File: stellar_toml.py    From py-stellar-base with Apache License 2.0 5 votes vote down vote up
def fetch_stellar_toml(
    domain: str,
    client: Union[BaseAsyncClient, BaseSyncClient] = None,
    use_http: bool = False,
) -> Union[Coroutine[Any, Any, Dict[str, Any]], Dict[str, Any]]:
    """Retrieve the stellar.toml file from a given domain.

    Retrieve the stellar.toml file for information about interacting with
    Stellar's federation protocol for a given Stellar Anchor (specified by a
    domain).

    :param domain: The domain the .toml file is hosted at.
    :param use_http: Specifies whether the request should go over plain HTTP vs HTTPS.
        Note it is recommend that you *always* use HTTPS.
    :param client: Http Client used to send the request.
    :return: The stellar.toml file as a an object via :func:`toml.loads`.
    :raises: :exc:`StellarTomlNotFoundError <stellar_sdk.sep.exceptions.StellarTomlNotFoundError>`:
        if the Stellar toml file could not not be found.
    """
    if not client:
        client = RequestsClient()

    toml_link = "/.well-known/stellar.toml"
    protocol = "https://"
    if use_http:
        protocol = "http://"
    url = protocol + domain + toml_link

    if isinstance(client, BaseAsyncClient):
        return __fetch_async(url, client)
    elif isinstance(client, BaseSyncClient):
        return __fetch_sync(url, client)
    else:
        raise TypeError(
            "This `client` class should be an instance "
            "of `stellar_sdk.client.base_async_client.BaseAsyncClient` "
            "or `stellar_sdk.client.base_sync_client.BaseSyncClient`."
        ) 
Example #25
Source File: server.py    From py-stellar-base with Apache License 2.0 5 votes vote down vote up
def close(self) -> Union[None, Coroutine[Any, Any, None]]:
        """Close underlying connector.

        Release all acquired resources.
        """
        if self.__async:
            return self.__close_async()
        else:
            return self.__close_sync() 
Example #26
Source File: server.py    From py-stellar-base with Apache License 2.0 5 votes vote down vote up
def fetch_base_fee(self) -> Union[int, Coroutine[Any, Any, int]]:
        """Fetch the base fee. Since this hits the server, if the server call fails,
        you might get an error. You should be prepared to use a default value if that happens.

        :return: the base fee
        :raises:
            :exc:`ConnectionError <stellar_sdk.exceptions.ConnectionError>`
            :exc:`NotFoundError <stellar_sdk.exceptions.NotFoundError>`
            :exc:`BadRequestError <stellar_sdk.exceptions.BadRequestError>`
            :exc:`BadResponseError <stellar_sdk.exceptions.BadResponseError>`
            :exc:`UnknownRequestError <stellar_sdk.exceptions.UnknownRequestError>`
        """
        if self.__async:
            return self.__fetch_base_fee_async()
        return self.__fetch_base_fee_sync() 
Example #27
Source File: connection.py    From vibora with MIT License 5 votes vote down vote up
def read_until(self, delimiter: bytes) -> Coroutine:
        """

        :param delimiter:
        :return:
        """
        return self.reader.readuntil(delimiter) 
Example #28
Source File: server.py    From py-stellar-base with Apache License 2.0 5 votes vote down vote up
def submit_transaction(
        self,
        transaction_envelope: Union[
            TransactionEnvelope, FeeBumpTransactionEnvelope, str
        ],
        skip_memo_required_check: bool = False,
    ) -> Union[Dict[str, Any], Coroutine[Any, Any, Dict[str, Any]]]:
        """Submits a transaction to the network.

        :param transaction_envelope: :class:`stellar_sdk.transaction_envelope.TransactionEnvelope` object
            or base64 encoded xdr
        :return: the response from horizon
        :raises:
            :exc:`ConnectionError <stellar_sdk.exceptions.ConnectionError>`
            :exc:`NotFoundError <stellar_sdk.exceptions.NotFoundError>`
            :exc:`BadRequestError <stellar_sdk.exceptions.BadRequestError>`
            :exc:`BadResponseError <stellar_sdk.exceptions.BadResponseError>`
            :exc:`UnknownRequestError <stellar_sdk.exceptions.UnknownRequestError>`
            :exc:`AccountRequiresMemoError <stellar_sdk.sep.exceptions.AccountRequiresMemoError>`
        """
        if self.__async:
            return self.__submit_transaction_async(
                transaction_envelope, skip_memo_required_check
            )
        return self.__submit_transaction_sync(
            transaction_envelope, skip_memo_required_check
        ) 
Example #29
Source File: task_runners.py    From async-worker with MIT License 5 votes vote down vote up
def __init__(
        self,
        seconds: int,
        task: Callable[["App"], Coroutine],
        app: "App",
        max_concurrency: int,
    ) -> None:
        self.seconds = seconds
        self.max_concurrency = max_concurrency
        self.task = task
        self.app = app
        self.running_tasks: Set[asyncio.Future] = set()
        self.task_is_done_event = asyncio.Event()
        self._started = False
        self.clock = ClockTicker(seconds=self.seconds) 
Example #30
Source File: test_adapter.py    From botbuilder-python with MIT License 5 votes vote down vote up
def __init__(
        self,
        logic: Coroutine = None,
        template_or_conversation: Union[Activity, ConversationReference] = None,
        send_trace_activities: bool = False,
    ):
        """
        Creates a new TestAdapter instance.
        :param logic:
        :param conversation: A reference to the conversation to begin the adapter state with.
        """
        super(TestAdapter, self).__init__()
        self.logic = logic
        self._next_id: int = 0
        self._user_tokens: List[UserToken] = []
        self._magic_codes: List[TokenMagicCode] = []
        self._conversation_lock = Lock()
        self.exchangeable_tokens: Dict[str, ExchangeableToken] = {}
        self.activity_buffer: List[Activity] = []
        self.updated_activities: List[Activity] = []
        self.deleted_activities: List[ConversationReference] = []
        self.send_trace_activities = send_trace_activities

        self.template = (
            template_or_conversation
            if isinstance(template_or_conversation, Activity)
            else Activity(
                channel_id="test",
                service_url="https://test.com",
                from_property=ChannelAccount(id="User1", name="user"),
                recipient=ChannelAccount(id="bot", name="Bot"),
                conversation=ConversationAccount(id="Convo1"),
            )
        )

        if isinstance(template_or_conversation, ConversationReference):
            self.template.channel_id = template_or_conversation.channel_id