本文整理汇总了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)
示例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
示例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