本文整理匯總了Python中typing.AsyncIterable方法的典型用法代碼示例。如果您正苦於以下問題:Python typing.AsyncIterable方法的具體用法?Python typing.AsyncIterable怎麽用?Python typing.AsyncIterable使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類typing
的用法示例。
在下文中一共展示了typing.AsyncIterable方法的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。
示例1: to_async_iterable
# 需要導入模塊: import typing [as 別名]
# 或者: from typing import AsyncIterable [as 別名]
def to_async_iterable(
self, maxsize: int, return_index: bool
) -> tp.AsyncIterable[T]:
# build stages first to verify reuse
main_queue: IterableQueue[pypeln_utils.Element] = IterableQueue(
maxsize=maxsize, total_sources=1,
)
built = {}
workers: tp.List[Worker] = list(self.build(built, main_queue, main_queue))
supervisor = Supervisor(workers=workers, main_queue=main_queue)
async with supervisor:
async for elem in main_queue:
if return_index:
yield elem
else:
yield elem.value
示例2: test_subclassing_async_generator
# 需要導入模塊: import typing [as 別名]
# 或者: from typing import AsyncIterable [as 別名]
def test_subclassing_async_generator(self):
class G(typing.AsyncGenerator[int, int]):
def asend(self, value):
pass
def athrow(self, typ, val=None, tb=None):
pass
ns = {}
exec('async def g(): yield 0', globals(), ns)
g = ns['g']
self.assertIsSubclass(G, typing.AsyncGenerator)
self.assertIsSubclass(G, typing.AsyncIterable)
self.assertIsSubclass(G, collections.AsyncGenerator)
self.assertIsSubclass(G, collections.AsyncIterable)
self.assertNotIsSubclass(type(g), G)
instance = G()
self.assertIsInstance(instance, typing.AsyncGenerator)
self.assertIsInstance(instance, typing.AsyncIterable)
self.assertIsInstance(instance, collections.AsyncGenerator)
self.assertIsInstance(instance, collections.AsyncIterable)
self.assertNotIsInstance(type(g), G)
self.assertNotIsInstance(g, G)
示例3: get_async_response
# 需要導入模塊: import typing [as 別名]
# 或者: from typing import AsyncIterable [as 別名]
def get_async_response(self, data: dict, batch_size: int) -> AsyncIterable:
"""Helper function for sending requests asynchronously if the API endpoint does not support batching
Args:
data: data to be passed to the API endpoint
batch_size: requests count
Yields:
requests results parsed as json
"""
loop = asyncio.get_event_loop()
futures = [
loop.run_in_executor(
None,
requests.post,
self.url,
None,
{k: v[i] for k, v in data.items()}
)
for i in range(batch_size)
]
for r in await asyncio.gather(*futures):
yield r.json()
示例4: _inner_messages
# 需要導入模塊: import typing [as 別名]
# 或者: from typing import AsyncIterable [as 別名]
def _inner_messages(self, ws: websockets.WebSocketClientProtocol) -> AsyncIterable[str]:
# Terminate the recv() loop as soon as the next message timed out, so the outer loop can reconnect.
try:
while True:
try:
msg: str = await asyncio.wait_for(ws.recv(), timeout=self.MESSAGE_TIMEOUT)
yield msg
except asyncio.TimeoutError:
try:
pong_waiter = await ws.ping()
await asyncio.wait_for(pong_waiter, timeout=self.PING_TIMEOUT)
except asyncio.TimeoutError:
raise
except asyncio.TimeoutError:
self.logger().warning("WebSocket ping timed out. Going to reconnect...")
return
except ConnectionClosed:
return
finally:
await ws.close()
示例5: _messages
# 需要導入模塊: import typing [as 別名]
# 或者: from typing import AsyncIterable [as 別名]
def _messages(self) -> AsyncIterable[Any]:
try:
while True:
try:
raw_msg_str: str = await asyncio.wait_for(self._client.recv(), timeout=self.MESSAGE_TIMEOUT)
raw_msg = ujson.loads(raw_msg_str)
yield raw_to_response(raw_msg)
except asyncio.TimeoutError:
try:
pong_waiter = await self._client.ping()
await asyncio.wait_for(pong_waiter, timeout=self.PING_TIMEOUT)
except asyncio.TimeoutError:
raise
except asyncio.TimeoutError:
self.logger().warning("WebSocket ping timed out. Going to reconnect...")
return
except ConnectionClosed:
return
finally:
await self.disconnect()
# emit messages
示例6: aiter_to_list
# 需要導入模塊: import typing [as 別名]
# 或者: from typing import AsyncIterable [as 別名]
def aiter_to_list(aiter: AsyncIterable[T],) -> List[T]:
return [x async for x in aiter]
示例7: upload_media
# 需要導入模塊: import typing [as 別名]
# 或者: from typing import AsyncIterable [as 別名]
def upload_media(self, data: Union[bytes, AsyncIterable[bytes]],
mime_type: Optional[str] = None, filename: Optional[str] = None,
size: Optional[int] = None) -> ContentURI:
"""
Upload a file to the content repository.
See also: `API reference <https://matrix.org/docs/spec/client_server/r0.4.0.html#post-matrix-media-r0-upload>`__
Args:
data: The data to upload.
mime_type: The MIME type to send with the upload request.
filename: The filename to send with the upload request.
size: The file size to send with the upload request.
Returns:
The MXC URI to the uploaded file.
Raises:
MatrixResponseError: If the response does not contain a ``content_uri`` field.
"""
if magic and isinstance(data, bytes):
mime_type = mime_type or magic.from_buffer(data, mime=True)
headers = {}
if mime_type:
headers["Content-Type"] = mime_type
if size:
headers["Content-Length"] = str(size)
query = {}
if filename:
query["filename"] = filename
resp = await self.api.request(Method.POST, MediaPath.upload, content=data,
headers=headers, query_params=query)
try:
return resp["content_uri"]
except KeyError:
raise MatrixResponseError("`content_uri` not in response.")
示例8: from_async_iterable
# 需要導入模塊: import typing [as 別名]
# 或者: from typing import AsyncIterable [as 別名]
def from_async_iterable(iterable: AsyncIterable[T]) -> AsyncObservable[T]:
"""Convert an async iterable to a source stream.
2 - xs = from_async_iterable(async_iterable)
Returns the source stream whose elements are pulled from the
given (async) iterable sequence."""
return FromAsyncIterable(iterable)
示例9: to_async_iterable
# 需要導入模塊: import typing [as 別名]
# 或者: from typing import AsyncIterable [as 別名]
def to_async_iterable(source: AsyncObservable) -> AsyncIterable:
"""Skip the specified number of values.
Keyword arguments:
count -- The number of elements to skip before returning the
remaining values.
Returns a source stream that contains the values that occur
after the specified index in the input source stream.
"""
return ToAsyncIterable(source)
示例10: repeat
# 需要導入模塊: import typing [as 別名]
# 或者: from typing import AsyncIterable [as 別名]
def repeat(value, times=None) -> AsyncIterable:
for value in itertools.repeat(value, times):
yield value
示例11: range
# 需要導入模塊: import typing [as 別名]
# 或者: from typing import AsyncIterable [as 別名]
def range(*args) -> AsyncIterable:
for value in builtins.range(*args):
yield value
示例12: filter
# 需要導入模塊: import typing [as 別名]
# 或者: from typing import AsyncIterable [as 別名]
def filter(predicate, source: AsyncIterable) -> AsyncIterable:
async for value in source:
if predicate(value):
yield value
示例13: __init__
# 需要導入模塊: import typing [as 別名]
# 或者: from typing import AsyncIterable [as 別名]
def __init__(self, source: AsyncIterable) -> None:
self._source = source
示例14: to_async_observable
# 需要導入模塊: import typing [as 別名]
# 或者: from typing import AsyncIterable [as 別名]
def to_async_observable(source: AsyncIterable) -> AsyncObservable:
"""Convert to async observable.
Keyword arguments:
source -- Async iterable to convert to async observable.
Returns async observable"""
return ToAsyncObservable(source)
示例15: to_async_iterable
# 需要導入模塊: import typing [as 別名]
# 或者: from typing import AsyncIterable [as 別名]
def to_async_iterable() -> Callable[[AsyncObservable], AsyncIterable]:
from aioreactive.operators.to_async_iterable import to_async_iterable
return partial(to_async_iterable)