当前位置: 首页>>代码示例>>Python>>正文


Python asyncio.get_event_loop_policy方法代码示例

本文整理汇总了Python中asyncio.get_event_loop_policy方法的典型用法代码示例。如果您正苦于以下问题:Python asyncio.get_event_loop_policy方法的具体用法?Python asyncio.get_event_loop_policy怎么用?Python asyncio.get_event_loop_policy使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在asyncio的用法示例。


在下文中一共展示了asyncio.get_event_loop_policy方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: _patch_asyncio

# 需要导入模块: import asyncio [as 别名]
# 或者: from asyncio import get_event_loop_policy [as 别名]
def _patch_asyncio() -> None:
    # This patches asyncio to add a sync_wait method to the event
    # loop. This method can then be called from within a task
    # including a synchronous function called from a task. Sadly it
    # requires the python Task and Future implementations, which
    # invokes some performance cost.
    asyncio.Task = asyncio.tasks._CTask = asyncio.tasks.Task = asyncio.tasks._PyTask  # type: ignore
    asyncio.Future = (  # type: ignore
        asyncio.futures._CFuture  # type: ignore
    ) = asyncio.futures.Future = asyncio.futures._PyFuture  # type: ignore # noqa

    current_policy = asyncio.get_event_loop_policy()
    if hasattr(asyncio, "unix_events"):
        target_policy = asyncio.unix_events._UnixDefaultEventLoopPolicy
    else:
        target_policy = object  # type: ignore

    if not isinstance(current_policy, target_policy):
        raise RuntimeError("Flask Patching only works with the default event loop policy")

    _patch_loop()
    _patch_task() 
开发者ID:pgjones,项目名称:quart,代码行数:24,代码来源:_patch.py

示例2: _init_asyncio_patch

# 需要导入模块: import asyncio [as 别名]
# 或者: from asyncio import get_event_loop_policy [as 别名]
def _init_asyncio_patch():
    """
    Select compatible event loop for Tornado 5+.
    As of Python 3.8, the default event loop on Windows is `proactor`,
    however Tornado requires the old default "selector" event loop.
    As Tornado has decided to leave this to users to set, MkDocs needs
    to set it. See https://github.com/tornadoweb/tornado/issues/2608.
    """
    if sys.platform.startswith("win") and sys.version_info >= (3, 8):
        import asyncio
        try:
            from asyncio import WindowsSelectorEventLoopPolicy
        except ImportError:
            pass  # Can't assign a policy which doesn't exist.
        else:
            if not isinstance(asyncio.get_event_loop_policy(), WindowsSelectorEventLoopPolicy):
                asyncio.set_event_loop_policy(WindowsSelectorEventLoopPolicy()) 
开发者ID:tableau,项目名称:TabPy,代码行数:19,代码来源:app.py

示例3: event_loop

# 需要导入模块: import asyncio [as 别名]
# 或者: from asyncio import get_event_loop_policy [as 别名]
def event_loop(request):
    """
    Create an instance of the requested event loop.
    """
    default_event_loop(request=request)

    # Close previous event loop
    policy = asyncio.get_event_loop_policy()
    policy.get_event_loop().close()

    # Create new event loop
    _event_loop = policy.new_event_loop()
    policy.set_event_loop(_event_loop)

    def fin():
        _event_loop.close()

    # Add finaliser and return new event loop
    request.addfinalizer(fin)
    return _event_loop 
开发者ID:saltyrtc,项目名称:saltyrtc-server-python,代码行数:22,代码来源:conftest.py

示例4: event_loop

# 需要导入模块: import asyncio [as 别名]
# 或者: from asyncio import get_event_loop_policy [as 别名]
def event_loop(request):
    """Overwrite `pytest_asyncio` eventloop to fix Windows issue.

    Default implementation causes `NotImplementedError` on Windows with
    Python 3.8, because they changed default eventloop in 3.8.

    NOTE: We do the same thing in the `example/settings.py` because it
    imports (and fails) before we have a chance to invoke this fixture.
    So, we could avoid adding this fixture, but I feel it is better to
    keep the proper solution here as well.

    """
    if sys.platform == "win32" and sys.version_info.minor >= 8:
        asyncio.set_event_loop_policy(
            asyncio.WindowsSelectorEventLoopPolicy()  # pylint: disable=no-member
        )
    loop = asyncio.get_event_loop_policy().new_event_loop()
    yield loop
    loop.close() 
开发者ID:datadvance,项目名称:DjangoChannelsGraphqlWs,代码行数:21,代码来源:conftest.py

示例5: __init__

# 需要导入模块: import asyncio [as 别名]
# 或者: from asyncio import get_event_loop_policy [as 别名]
def __init__(self, conf: Config, analytics_manager=None, skip_components=None,
                 peer_manager=None, **override_components):
        self.conf = conf
        self.skip_components = skip_components or []
        self.loop = asyncio.get_event_loop()
        self.analytics_manager = analytics_manager
        self.component_classes = {}
        self.components = set()
        self.started = asyncio.Event(loop=self.loop)
        self.peer_manager = peer_manager or PeerManager(asyncio.get_event_loop_policy().get_event_loop())

        for component_name, component_class in self.default_component_classes.items():
            if component_name in override_components:
                component_class = override_components.pop(component_name)
            if component_name not in self.skip_components:
                self.component_classes[component_name] = component_class

        if override_components:
            raise SyntaxError("unexpected components: %s" % override_components)

        for component_class in self.component_classes.values():
            self.components.add(component_class(self)) 
开发者ID:lbryio,项目名称:lbry-sdk,代码行数:24,代码来源:componentmanager.py

示例6: _init_asyncio_patch

# 需要导入模块: import asyncio [as 别名]
# 或者: from asyncio import get_event_loop_policy [as 别名]
def _init_asyncio_patch():
    """
    Select compatible event loop for Tornado 5+.

    As of Python 3.8, the default event loop on Windows is `proactor`,
    however Tornado requires the old default "selector" event loop.
    As Tornado has decided to leave this to users to set, MkDocs needs
    to set it. See https://github.com/tornadoweb/tornado/issues/2608.
    """
    if sys.platform.startswith("win") and sys.version_info >= (3, 8):
        import asyncio
        try:
            from asyncio import WindowsSelectorEventLoopPolicy
        except ImportError:
            pass  # Can't assign a policy which doesn't exist.
        else:
            if not isinstance(asyncio.get_event_loop_policy(), WindowsSelectorEventLoopPolicy):
                asyncio.set_event_loop_policy(WindowsSelectorEventLoopPolicy()) 
开发者ID:mkdocs,项目名称:mkdocs,代码行数:20,代码来源:serve.py

示例7: runner

# 需要导入模块: import asyncio [as 别名]
# 或者: from asyncio import get_event_loop_policy [as 别名]
def runner(request):
    def _( ga, **kargs ):
        time.sleep(0.5) # leave the time to shutdown previous instance
        if request.param=="serve":
            return getattr(ga,request.param)(port=10000)
        else:
            return getattr(ga,request.param)(**kargs)

    return _

# @pytest.yield_fixture(scope='session')
# def event_loop(request):
#     """Create an instance of the default event loop for each test case."""
#     loop = asyncio.get_event_loop_policy().new_event_loop()
#     yield loop
#     loop.close() 
开发者ID:manatlan,项目名称:guy,代码行数:18,代码来源:conftest.py

示例8: test_get_event_loop_returns_running_loop

# 需要导入模块: import asyncio [as 别名]
# 或者: from asyncio import get_event_loop_policy [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

示例9: event_loop

# 需要导入模块: import asyncio [as 别名]
# 或者: from asyncio import get_event_loop_policy [as 别名]
def event_loop(request):
    """Create an instance of the default event loop for each test case.
    Also catches all warnings and raises exception if there was
    'coroutine was never awaited' wargning.
    """
    loop = asyncio.get_event_loop_policy().new_event_loop()

    with warnings.catch_warnings(record=True) as catched_warnings:
        yield loop
        # Collecting garbage should trigger warning for non-awaited coroutines
        gc.collect()

    for w in catched_warnings:
        if (
                isinstance(w.message, RuntimeWarning) and
                str(w.message).endswith('was never awaited')
        ):
            raise w.message
        else:
            warnings.showwarning(w.message, w.category, w.filename, w.lineno)

    loop.close() 
开发者ID:anti-social,项目名称:elasticmagic,代码行数:24,代码来源:conftest.py

示例10: test_event_loop_policy

# 需要导入模块: import asyncio [as 别名]
# 或者: from asyncio import get_event_loop_policy [as 别名]
def test_event_loop_policy(caplog, policy, policy_name):
    """Test that a the runner switches to a different event loop policy when instructed to."""
    caplog.set_level(logging.INFO)
    component = ShutdownComponent()
    old_policy = asyncio.get_event_loop_policy()
    try:
        run_application(component, event_loop_policy=policy)
    except DistributionNotFound as e:
        pytest.skip(str(e))
    finally:
        asyncio.set_event_loop_policy(old_policy)

    records = [record for record in caplog.records if record.name == 'asphalt.core.runner']
    assert len(records) == 6
    assert records[0].message == 'Running in development mode'
    assert records[1].message == 'Switched event loop policy to %s' % policy_name
    assert records[2].message == 'Starting application'
    assert records[3].message == 'Application started'
    assert records[4].message == 'Stopping application'
    assert records[5].message == 'Application stopped' 
开发者ID:asphalt-framework,项目名称:asphalt,代码行数:22,代码来源:test_runner.py

示例11: event_loop

# 需要导入模块: import asyncio [as 别名]
# 或者: from asyncio import get_event_loop_policy [as 别名]
def event_loop(request):
    loop = asyncio.get_event_loop_policy().new_event_loop()
    yield loop
    loop.close() 
开发者ID:scrapbird,项目名称:sarlacc,代码行数:6,代码来源:test_storage.py

示例12: check

# 需要导入模块: import asyncio [as 别名]
# 或者: from asyncio import get_event_loop_policy [as 别名]
def check(self, src, value):
        policy = asyncio.get_event_loop_policy()
        loop = policy.new_event_loop()
        asyncio.set_event_loop_policy(ForbiddenEventLoopPolicy())
        try:
            loop.run_until_complete(self._check(src, value, loop))
        finally:
            loop.close()
            asyncio.set_event_loop_policy(policy) 
开发者ID:vmagamedov,项目名称:hiku,代码行数:11,代码来源:test_source_aiopg.py

示例13: event_loop

# 需要导入模块: import asyncio [as 别名]
# 或者: from asyncio import get_event_loop_policy [as 别名]
def event_loop():
    from moler.asyncio_runner import cancel_remaining_feeders

    loop = asyncio.get_event_loop_policy().new_event_loop()
    yield loop
    # event_loop fixture is autoloaded by @pytest.mark.asyncio decorator
    # and inside some of our async tests we just submit() observer inside runner without stopping it
    # so, we need to stop all submitted futures
    cancel_remaining_feeders(loop)
    loop.close() 
开发者ID:nokia,项目名称:moler,代码行数:12,代码来源:py3test_runners.py

示例14: event_loop

# 需要导入模块: import asyncio [as 别名]
# 或者: from asyncio import get_event_loop_policy [as 别名]
def event_loop(request):
    """Create an instance of the default event loop for each test case."""
    loop = asyncio.get_event_loop_policy().new_event_loop()
    yield loop
    loop.close() 
开发者ID:manatlan,项目名称:reqman,代码行数:7,代码来源:conftest.py

示例15: setUp

# 需要导入模块: import asyncio [as 别名]
# 或者: from asyncio import get_event_loop_policy [as 别名]
def setUp(self):
        self.old = dict(_SCENARIO)
        self.oldsetup = dict(_FIXTURES)
        util._STOP = False
        util._TIMER = None
        self.policy = asyncio.get_event_loop_policy() 
开发者ID:loads,项目名称:molotov,代码行数:8,代码来源:support.py


注:本文中的asyncio.get_event_loop_policy方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。