当前位置: 首页>>代码示例>>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;未经允许,请勿转载。