當前位置: 首頁>>代碼示例>>Python>>正文


Python asyncio._get_running_loop方法代碼示例

本文整理匯總了Python中asyncio._get_running_loop方法的典型用法代碼示例。如果您正苦於以下問題:Python asyncio._get_running_loop方法的具體用法?Python asyncio._get_running_loop怎麽用?Python asyncio._get_running_loop使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在asyncio的用法示例。


在下文中一共展示了asyncio._get_running_loop方法的10個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。

示例1: run_main

# 需要導入模塊: import asyncio [as 別名]
# 或者: from asyncio import _get_running_loop [as 別名]
def run_main(main):
    """Main entrypoint for asyncio tasks.

    This differs from `asyncio.run` in one key way - the main task is cancelled
    *first*, then any outstanding tasks are cancelled (and logged, remaining
    tasks are indicative of bugs/failures).
    """
    if asyncio._get_running_loop() is not None:
        raise RuntimeError("Cannot be called from inside a running event loop")

    main_task = None
    loop = asyncio.new_event_loop()
    try:
        asyncio.set_event_loop(loop)
        main_task = loop.create_task(main)
        return loop.run_until_complete(main_task)
    finally:
        try:
            if main_task is not None:
                loop.run_until_complete(cancel_task(main_task))
            _cancel_all_tasks(loop)
            loop.run_until_complete(loop.shutdown_asyncgens())
        finally:
            asyncio.set_event_loop(None)
            loop.close() 
開發者ID:dask,項目名稱:dask-gateway,代碼行數:27,代碼來源:utils.py

示例2: test_get_event_loop_returns_running_loop

# 需要導入模塊: import asyncio [as 別名]
# 或者: from asyncio import _get_running_loop [as 別名]
def test_get_event_loop_returns_running_loop(self):
        class Policy(asyncio.DefaultEventLoopPolicy):
            def get_event_loop(self):
                raise NotImplementedError

        loop = None

        old_policy = asyncio.get_event_loop_policy()
        try:
            asyncio.set_event_loop_policy(Policy())
            loop = asyncio.new_event_loop()
            self.assertIs(asyncio._get_running_loop(), None)

            async def func():
                self.assertIs(asyncio.get_event_loop(), loop)
                self.assertIs(asyncio._get_running_loop(), loop)

            loop.run_until_complete(func())
        finally:
            asyncio.set_event_loop_policy(old_policy)
            if loop is not None:
                loop.close()

        self.assertIs(asyncio._get_running_loop(), None) 
開發者ID:ShikyoKira,項目名稱:Project-New-Reign---Nemesis-Main,代碼行數:26,代碼來源:test_events.py

示例3: get_running_loop

# 需要導入模塊: import asyncio [as 別名]
# 或者: from asyncio import _get_running_loop [as 別名]
def get_running_loop():
    """Gets the currently running event loop

    Uses asyncio.get_running_loop() if available (Python 3.7+) or a backported
    version of the same function in 3.5/3.6.
    """
    try:
        loop = asyncio.get_running_loop()
    except AttributeError:
        loop = asyncio._get_running_loop()
        if loop is None:
            raise RuntimeError("no running event loop")
    return loop 
開發者ID:Azure,項目名稱:azure-iot-sdk-python,代碼行數:15,代碼來源:asyncio_compat.py

示例4: acquire_loop

# 需要導入模塊: import asyncio [as 別名]
# 或者: from asyncio import _get_running_loop [as 別名]
def acquire_loop(running: bool = False) -> asyncio.AbstractEventLoop:
    """Gracefully acquire a loop.

    The function tries to get an event loop via :func:`asyncio.get_event_loop`.
    On fail, returns a new loop using :func:`asyncio.new_event_loop`.

    Parameters
    ----------
    running: :class:`bool`
        Indicates if the function should get a loop that is already running.
    """
    try:
        loop = asyncio._get_running_loop()

    except Exception:  # an error might occur actually
        loop = None

    if running and loop is not None:
        return loop

    else:
        try:
            loop = asyncio.get_event_loop()

            if loop.is_running() and not running:
                # loop is running while we have to get the non-running one,
                # let us raise an error to go into <except> clause.
                raise ValueError("Current event loop is already running.")

        except Exception:
            loop = asyncio.new_event_loop()

    return loop 
開發者ID:NeKitDS,項目名稱:gd.py,代碼行數:35,代碼來源:async_utils.py

示例5: _get_running_loop

# 需要導入模塊: import asyncio [as 別名]
# 或者: from asyncio import _get_running_loop [as 別名]
def _get_running_loop():
            return _running_loop._loop 
開發者ID:open-telemetry,項目名稱:opentelemetry-python,代碼行數:4,代碼來源:aiocontextvarsfix.py

示例6: _get_event_loop

# 需要導入模塊: import asyncio [as 別名]
# 或者: from asyncio import _get_running_loop [as 別名]
def _get_event_loop():
            current_loop = _get_running_loop()
            if current_loop is not None:
                return current_loop
            return asyncio.events.get_event_loop_policy().get_event_loop() 
開發者ID:open-telemetry,項目名稱:opentelemetry-python,代碼行數:7,代碼來源:aiocontextvarsfix.py

示例7: _get_running_loop

# 需要導入模塊: import asyncio [as 別名]
# 或者: from asyncio import _get_running_loop [as 別名]
def _get_running_loop():
                return _running_loop._loop 
開發者ID:eventbrite,項目名稱:pysoa,代碼行數:4,代碼來源:compatibility.py

示例8: _get_event_loop

# 需要導入模塊: import asyncio [as 別名]
# 或者: from asyncio import _get_running_loop [as 別名]
def _get_event_loop():
                current_loop = _get_running_loop()
                if current_loop is not None:
                    return current_loop
                return asyncio.events.get_event_loop_policy().get_event_loop() 
開發者ID:eventbrite,項目名稱:pysoa,代碼行數:7,代碼來源:compatibility.py

示例9: close

# 需要導入模塊: import asyncio [as 別名]
# 或者: from asyncio import _get_running_loop [as 別名]
def close(self) -> None:
        """
        關閉天勤接口實例並釋放相應資源

        Example::

            # m1901開多3手
            from tqsdk import TqApi
            from contextlib import closing

            with closing(TqApi()) as api:
                api.insert_order(symbol="DCE.m1901", direction="BUY", offset="OPEN", volume=3)
        """
        if self._loop.is_closed():
            return
        if self._loop.is_running():
            raise Exception("不能在協程中調用 close, 如需關閉 api 實例需在 wait_update 返回後再關閉")
        elif asyncio._get_running_loop():
            raise Exception(
                "TqSdk 使用了 python3 的原生協程和異步通訊庫 asyncio,您所使用的 IDE 不支持 asyncio, 請使用 pycharm 或其它支持 asyncio 的 IDE")

        # 總會發送 serial_extra_array 數據,由 TqWebHelper 處理
        for _, serial in self._serials.items():
            self._process_serial_extra_array(serial)
        self._run_until_idle()  # 由於有的處於 ready 狀態 task 可能需要報撤單, 因此一直運行到沒有 ready 狀態的 task
        for task in self._tasks:
            task.cancel()
        while self._tasks:  # 等待 task 執行完成
            self._run_once()
        self._loop.run_until_complete(self._loop.shutdown_asyncgens())
        self._loop.close()
        _clear_logs()  # 清除過期日誌文件 
開發者ID:shinnytech,項目名稱:tqsdk-python,代碼行數:34,代碼來源:api.py

示例10: sync_wrapper

# 需要導入模塊: import asyncio [as 別名]
# 或者: from asyncio import _get_running_loop [as 別名]
def sync_wrapper(coro):
    def inner_sync_wrapper(self, *args, **kwargs):
        start_new_loop = None
        # try to get the running loop
        # `get_running_loop()` is new in Python 3.7, fall back on privateinternal for 3.6
        try:
            get_running_loop = asyncio.get_running_loop
        except AttributeError:
            get_running_loop = asyncio._get_running_loop

        # If there is no running loop we will need to start a new one and run it to completion
        try:
            if get_running_loop():
                start_new_loop = False
            else:
                start_new_loop = True
        except RuntimeError:
            start_new_loop = True

        if start_new_loop is True:
            f = asyncio.ensure_future(coro(self, *args, **kwargs))
            asyncio.get_event_loop().run_until_complete(f)
            f = f.result()
        else:
            # don't use create_task. It's python3.7 only
            f = asyncio.ensure_future(coro(self, *args, **kwargs))

        return f

    return inner_sync_wrapper

# Monkey patch in our sync_wrapper 
開發者ID:FlorianKempenich,項目名稱:Appdaemon-Test-Framework,代碼行數:34,代碼來源:__init__.py


注:本文中的asyncio._get_running_loop方法示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。