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


Python abc.Awaitable方法代码示例

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


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

示例1: handle_request

# 需要导入模块: from collections import abc [as 别名]
# 或者: from collections.abc import Awaitable [as 别名]
def handle_request(self, source, msgID, method, *args, **kw):
        """
        Handle a request from source.
        """
        try:
            func = self.getRequestHandler(method, source=source, msgID=msgID)
            if 'timeout' in kw and not acceptArg(func, 'timeout'):
                del kw['timeout']
            if inspect.iscoroutinefunction(func):
                result = await func(*args, **kw)
            else:
                result = await self.loop.run_in_executor(
                    self.executor, functools.partial(func, *args, **kw))
            if isinstance(result, Awaitable):
                result = await result
        except QuLabRPCError as e:
            result = e
        except Exception as e:
            result = QuLabRPCServerError.make(e)
        msg = pack(result)
        await self.response(source, msgID, msg) 
开发者ID:feihoo87,项目名称:QuLab,代码行数:23,代码来源:rpc.py

示例2: test_Awaitable

# 需要导入模块: from collections import abc [as 别名]
# 或者: from collections.abc import Awaitable [as 别名]
def test_Awaitable(self):
        def gen():
            yield

        @types.coroutine
        def coro():
            yield

        async def new_coro():
            pass

        class Bar:
            def __await__(self):
                yield

        class MinimalCoro(Coroutine):
            def send(self, value):
                return value
            def throw(self, typ, val=None, tb=None):
                super().throw(typ, val, tb)
            def __await__(self):
                yield

        non_samples = [None, int(), gen(), object()]
        for x in non_samples:
            self.assertNotIsInstance(x, Awaitable)
            self.assertFalse(issubclass(type(x), Awaitable), repr(type(x)))

        samples = [Bar(), MinimalCoro()]
        for x in samples:
            self.assertIsInstance(x, Awaitable)
            self.assertTrue(issubclass(type(x), Awaitable))

        c = coro()
        # Iterable coroutines (generators with CO_ITERABLE_COROUTINE
        # flag don't have '__await__' method, hence can't be instances
        # of Awaitable. Use inspect.isawaitable to detect them.
        self.assertNotIsInstance(c, Awaitable)

        c = new_coro()
        self.assertIsInstance(c, Awaitable)
        c.close() # awoid RuntimeWarning that coro() was not awaited

        class CoroLike: pass
        Coroutine.register(CoroLike)
        self.assertTrue(isinstance(CoroLike(), Awaitable))
        self.assertTrue(issubclass(CoroLike, Awaitable))
        CoroLike = None
        support.gc_collect() # Kill CoroLike to clean-up ABCMeta cache 
开发者ID:Microvellum,项目名称:Fluid-Designer,代码行数:51,代码来源:test_collections.py

示例3: test_Awaitable

# 需要导入模块: from collections import abc [as 别名]
# 或者: from collections.abc import Awaitable [as 别名]
def test_Awaitable(self):
        def gen():
            yield

        @types.coroutine
        def coro():
            yield

        async def new_coro():
            pass

        class Bar:
            def __await__(self):
                yield

        class MinimalCoro(Coroutine):
            def send(self, value):
                return value
            def throw(self, typ, val=None, tb=None):
                super().throw(typ, val, tb)
            def __await__(self):
                yield

        non_samples = [None, int(), gen(), object()]
        for x in non_samples:
            self.assertNotIsInstance(x, Awaitable)
            self.assertFalse(issubclass(type(x), Awaitable), repr(type(x)))

        samples = [Bar(), MinimalCoro()]
        for x in samples:
            self.assertIsInstance(x, Awaitable)
            self.assertTrue(issubclass(type(x), Awaitable))

        c = coro()
        # Iterable coroutines (generators with CO_ITERABLE_COROUTINE
        # flag don't have '__await__' method, hence can't be instances
        # of Awaitable. Use inspect.isawaitable to detect them.
        self.assertNotIsInstance(c, Awaitable)

        c = new_coro()
        self.assertIsInstance(c, Awaitable)
        c.close() # avoid RuntimeWarning that coro() was not awaited

        class CoroLike: pass
        Coroutine.register(CoroLike)
        self.assertTrue(isinstance(CoroLike(), Awaitable))
        self.assertTrue(issubclass(CoroLike, Awaitable))
        CoroLike = None
        support.gc_collect() # Kill CoroLike to clean-up ABCMeta cache 
开发者ID:bkerler,项目名称:android_universal,代码行数:51,代码来源:test_collections.py


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