本文整理汇总了Python中async_generator.yield_方法的典型用法代码示例。如果您正苦于以下问题:Python async_generator.yield_方法的具体用法?Python async_generator.yield_怎么用?Python async_generator.yield_使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类async_generator
的用法示例。
在下文中一共展示了async_generator.yield_方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: purerpc_codegen_greeter_port
# 需要导入模块: import async_generator [as 别名]
# 或者: from async_generator import yield_ [as 别名]
def purerpc_codegen_greeter_port(greeter_pb2, greeter_grpc):
class Servicer(greeter_grpc.GreeterServicer):
async def SayHello(self, message):
return greeter_pb2.HelloReply(message="Hello, " + message.name)
@async_generator
async def SayHelloGoodbye(self, message):
await yield_(greeter_pb2.HelloReply(message="Hello, " + message.name))
await yield_(greeter_pb2.HelloReply(message="Goodbye, " + message.name))
async def SayHelloToManyAtOnce(self, messages):
names = []
async for message in messages:
names.append(message.name)
return greeter_pb2.HelloReply(message="Hello, " + ", ".join(names))
@async_generator
async def SayHelloToMany(self, messages):
async for message in messages:
await yield_(greeter_pb2.HelloReply(message="Hello, " + message.name))
with run_purerpc_service_in_process(Servicer().service) as port:
yield port
示例2: get_entities_iter
# 需要导入模块: import async_generator [as 别名]
# 或者: from async_generator import yield_ [as 别名]
def get_entities_iter(mode, in_list, client):
"""
Get a generator of entities to act on given a mode ('blacklist',
'whitelist') and an input from that mode. If whitelist, generator
will be asynchronous.
"""
# TODO change None to empty blacklist?
mode = mode.lower()
if mode == 'whitelist':
assert client is not None
async for ent in entities_from_str(client.get_input_entity, in_list):
await yield_(ent)
elif mode == 'blacklist':
assert client is not None
avoid = set()
async for eid in entities_from_str(client.get_peer_id, in_list):
avoid.add(eid)
# TODO Should this get_dialogs call be cached? How?
async for dialog in client.iter_dialogs():
if dialog.id not in avoid:
await yield_(dialog.input_entity)
示例3: send_message_receive_stream
# 需要导入模块: import async_generator [as 别名]
# 或者: from async_generator import yield_ [as 别名]
def send_message_receive_stream(server_url,
auth_token,
sender_id,
message):
payload = {
"sender": sender_id,
"message": message
}
url = "{}/webhooks/rest/webhook?stream=true&token={}".format(
server_url, auth_token)
# TODO: check if this properly receives UTF-8 data
async with aiohttp.ClientSession() as session:
async with session.post(url,
json=payload,
raise_for_status=True) as resp:
async for line in resp.content:
if line:
await yield_(json.loads(line.decode("utf-8")))
示例4: async_parse
# 需要导入模块: import async_generator [as 别名]
# 或者: from async_generator import yield_ [as 别名]
def async_parse(ifp, pb_cls, **kwargs):
"""Parse an async stream.
Args:
ifp (file-like object): input async stream.
pb_cls (protobuf.message.Message.__class__): The class object of
the protobuf message type encoded in the stream.
"""
with open(fileobj=ifp, mode='rb', **kwargs) as istream:
async for data in istream:
if isinstance(data, istream.delimiter_class()):
await yield_(data)
else:
pb_obj = pb_cls()
pb_obj.ParseFromString(data)
await yield_(pb_obj)
示例5: progress
# 需要导入模块: import async_generator [as 别名]
# 或者: from async_generator import yield_ [as 别名]
def progress(self):
while True:
if self.state_ispending():
await yield_({
"message": "Pending in queue...",
})
elif self.state_isrunning():
await yield_({
"message": "Cluster job running... waiting to connect",
})
return
else:
await yield_({
"message": "Unknown status...",
})
await gen.sleep(1)
示例6: test_function
# 需要导入模块: import async_generator [as 别名]
# 或者: from async_generator import yield_ [as 别名]
def test_function(self, expected_exc):
@context_teardown
async def start(ctx: Context):
nonlocal phase, received_exception
phase = 'started'
exc = await yield_()
phase = 'finished'
received_exception = exc
phase = received_exception = None
context = Context()
await start(context)
assert phase == 'started'
await context.close(expected_exc)
assert phase == 'finished'
assert received_exception == expected_exc
示例7: test_method
# 需要导入模块: import async_generator [as 别名]
# 或者: from async_generator import yield_ [as 别名]
def test_method(self, expected_exc):
class SomeComponent:
@context_teardown
async def start(self, ctx: Context):
nonlocal phase, received_exception
phase = 'started'
exc = await yield_()
phase = 'finished'
received_exception = exc
phase = received_exception = None
context = Context()
await SomeComponent().start(context)
assert phase == 'started'
await context.close(expected_exc)
assert phase == 'finished'
assert received_exception == expected_exc
示例8: _paginator
# 需要导入模块: import async_generator [as 别名]
# 或者: from async_generator import yield_ [as 别名]
def _paginator(self):
if self.cassandra_fut is None:
raise RuntimeError(
'Pagination should be done inside async context manager')
while (
self._deque or
not self._finish_event.is_set() or
self._exc is not None
):
if self._exc is not None:
raise self._exc
while self._deque:
await yield_(self._deque.popleft())
await asyncio.wait(
(
self._drain_event.wait(),
self._finish_event.wait(),
),
return_when=asyncio.FIRST_COMPLETED,
loop=self._loop
)
示例9: __aiter__
# 需要导入模块: import async_generator [as 别名]
# 或者: from async_generator import yield_ [as 别名]
def __aiter__(self):
if not self.client.is_async:
raise RuntimeError('Calling __aiter__ on an asynchronus client. Use :for: not :async for:')
while True:
index = 0
for _ in range(index, len(self.raw_data)):
await yield_(self.raw_data[index])
index += 1
if not await self.update_data():
break
示例10: SayHelloToMany
# 需要导入模块: import async_generator [as 别名]
# 或者: from async_generator import yield_ [as 别名]
def SayHelloToMany(self, input_messages):
async for message in input_messages:
await yield_(HelloReply(message=message.name))
示例11: SayHelloGoodbye
# 需要导入模块: import async_generator [as 别名]
# 或者: from async_generator import yield_ [as 别名]
def SayHelloGoodbye(self, message):
await yield_(greeter_pb2.HelloReply(message=message.name))
await yield_(greeter_pb2.HelloReply(message=message.name))
示例12: SayHelloToMany
# 需要导入模块: import async_generator [as 别名]
# 或者: from async_generator import yield_ [as 别名]
def SayHelloToMany(self, messages):
async for message in messages:
await yield_(greeter_pb2.HelloReply(message=message.name))
示例13: _service_wrapper
# 需要导入模块: import async_generator [as 别名]
# 或者: from async_generator import yield_ [as 别名]
def _service_wrapper(service=None, setup_fn=None, teardown_fn=None):
if setup_fn is not None:
await yield_(await setup_fn())
else:
await yield_(service)
if teardown_fn is not None:
await teardown_fn()
示例14: stream_to_async_iterator
# 需要导入模块: import async_generator [as 别名]
# 或者: from async_generator import yield_ [as 别名]
def stream_to_async_iterator(stream: GRPCProtoStream):
while True:
msg = await stream.receive_message()
if msg is None:
event = stream.end_stream_event
if isinstance(event, ResponseEnded):
raise_status(event.status)
return
await yield_(msg)
示例15: purerpc_echo_port
# 需要导入模块: import async_generator [as 别名]
# 或者: from async_generator import yield_ [as 别名]
def purerpc_echo_port(echo_pb2, echo_grpc):
class Servicer(echo_grpc.EchoServicer):
async def Echo(self, message):
return echo_pb2.EchoReply(data=message.data)
@async_generator
async def EchoTwoTimes(self, message):
await yield_(echo_pb2.EchoReply(data=message.data))
await yield_(echo_pb2.EchoReply(data=message.data))
@async_generator
async def EchoEachTime(self, messages):
async for message in messages:
await yield_(echo_pb2.EchoReply(data=message.data))
async def EchoLast(self, messages):
data = []
async for message in messages:
data.append(message.data)
return echo_pb2.EchoReply(data="".join(data))
@async_generator
async def EchoLastV2(self, messages):
data = []
async for message in messages:
data.append(message.data)
await yield_(echo_pb2.EchoReply(data="".join(data)))
with run_purerpc_service_in_process(Servicer().service) as port:
yield port