本文整理匯總了Python中typing.AsyncGenerator方法的典型用法代碼示例。如果您正苦於以下問題:Python typing.AsyncGenerator方法的具體用法?Python typing.AsyncGenerator怎麽用?Python typing.AsyncGenerator使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類typing
的用法示例。
在下文中一共展示了typing.AsyncGenerator方法的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。
示例1: _subscribe
# 需要導入模塊: import typing [as 別名]
# 或者: from typing import AsyncGenerator [as 別名]
def _subscribe(
self, document: DocumentNode, *args, **kwargs
) -> AsyncGenerator[ExecutionResult, None]:
# Fetch schema from transport if needed and validate document if possible
await self.fetch_and_validate(document)
# Subscribe to the transport
inner_generator: AsyncGenerator[
ExecutionResult, None
] = self.transport.subscribe(document, *args, **kwargs)
# Keep a reference to the inner generator to allow the user to call aclose()
# before a break if python version is too old (pypy3 py 3.6.1)
self._generator = inner_generator
async for result in inner_generator:
if result.errors:
# Note: we need to run generator.aclose() here or the finally block in
# transport.subscribe will not be reached in pypy3 (py 3.6.1)
await inner_generator.aclose()
yield result
示例2: subscribe
# 需要導入模塊: import typing [as 別名]
# 或者: from typing import AsyncGenerator [as 別名]
def subscribe(
self, document: DocumentNode, *args, **kwargs,
) -> AsyncGenerator[ExecutionResult, None]:
"""Send a subscription and receive the results using an async generator
The results are sent as an ExecutionResult object
"""
subscribe_result = await subscribe(self.schema, document, *args, **kwargs)
if isinstance(subscribe_result, ExecutionResult):
yield subscribe_result
else:
async for result in subscribe_result:
yield result
示例3: run_sync_iterable
# 需要導入模塊: import typing [as 別名]
# 或者: from typing import AsyncGenerator [as 別名]
def run_sync_iterable(iterable: Generator[Any, None, None]) -> AsyncGenerator[Any, None]:
async def _gen_wrapper() -> AsyncGenerator[Any, None]:
# Wrap the generator such that each iteration runs
# in the executor. Then rationalise the raised
# errors so that it ends.
def _inner() -> Any:
# https://bugs.python.org/issue26221
# StopIteration errors are swallowed by the
# run_in_exector method
try:
return next(iterable)
except StopIteration:
raise StopAsyncIteration()
loop = asyncio.get_running_loop()
while True:
try:
yield await loop.run_in_executor(None, copy_context().run, _inner)
except StopAsyncIteration:
return
return _gen_wrapper()
示例4: test_stream_with_context
# 需要導入模塊: import typing [as 別名]
# 或者: from typing import AsyncGenerator [as 別名]
def test_stream_with_context() -> None:
app = Quart(__name__)
@app.route("/")
async def index() -> AsyncGenerator[bytes, None]:
@stream_with_context
async def generator() -> AsyncGenerator[bytes, None]:
yield request.method.encode()
yield b" "
yield request.path.encode()
return generator()
test_client = app.test_client()
response = await test_client.get("/")
result = await response.get_data(raw=True)
assert result == b"GET /" # type: ignore
示例5: get
# 需要導入模塊: import typing [as 別名]
# 或者: from typing import AsyncGenerator [as 別名]
def get(
self, path: str, required_serial: Optional[int], **kw: Any
) -> AsyncGenerator[aiohttp.ClientResponse, None]:
logger.debug(f"Getting {path} (serial {required_serial})")
if not path.startswith(("https://", "http://")):
path = self.url + path
async with self.session.get(path, **kw) as r:
got_serial = (
int(r.headers[PYPI_SERIAL_HEADER])
if PYPI_SERIAL_HEADER in r.headers
else None
)
await self.check_for_stale_cache(path, required_serial, got_serial)
yield r
# TODO: Add storage backend support / refactor - #554
示例6: as_completed_with_async
# 需要導入模塊: import typing [as 別名]
# 或者: from typing import AsyncGenerator [as 別名]
def as_completed_with_async(
self,
coros: typing.Iterable[typing.Awaitable],
limit: int = 50,
raise_exception: bool = True,
) -> typing.AsyncGenerator[typing.Any, None]:
"""as_completed`s async version, can catch and log exception inside.
"""
for coro in self.as_completed(coros, limit=limit):
try:
yield await coro
except Exception as e:
if raise_exception:
raise e
else:
self.logger.exception(
"Get exception {:s} in "
'"as_completed_with_async"'.format(str(e))
)
示例7: __scan
# 需要導入模塊: import typing [as 別名]
# 或者: from typing import AsyncGenerator [as 別名]
def __scan(self, path: Union[str, os.PathLike], candidates: Set[str], similarity: float) -> AsyncGenerator[Tuple[str, pathlib.Path], None]:
"""One level depth search generator for application execs based on similarity with candidate names.
:param path: root dir of which subdirectories will be scanned
:param candidates: set of app names used for exact and close matching with directory names
:param similarity: cutoff level for difflib.get_close_matches; set 1 for exact matching only
:yields: 2-el. tuple of app_name and executable
"""
root, dirs, _ = next(os.walk(path))
logging.debug(f'New scan - similarity: {similarity}, candidates: {list(candidates)}')
for dir_name in dirs:
await asyncio.sleep(0)
matches = self.get_close_matches(dir_name, candidates, similarity)
for app_name in matches:
dir_path = pathlib.PurePath(root) / dir_name
best_exe = self.find_best_exe(dir_path, app_name)
if best_exe is None:
logging.warning('No executable found, moving to next best matched app')
continue
candidates.remove(app_name)
yield app_name, pathlib.Path(best_exe)
break
示例8: lock
# 需要導入模塊: import typing [as 別名]
# 或者: from typing import AsyncGenerator [as 別名]
def lock(
self,
conversation_id: Text,
lock_lifetime: float = LOCK_LIFETIME,
wait_time_in_seconds: float = 1,
) -> AsyncGenerator[TicketLock, None]:
"""Acquire lock with lifetime `lock_lifetime`for `conversation_id`.
Try acquiring lock with a wait time of `wait_time_in_seconds` seconds
between attempts. Raise a `LockError` if lock has expired.
"""
ticket = self.issue_ticket(conversation_id, lock_lifetime)
try:
yield await self._acquire_lock(
conversation_id, ticket, wait_time_in_seconds
)
finally:
self.cleanup(conversation_id, ticket)
示例9: test_subclassing_async_generator
# 需要導入模塊: import typing [as 別名]
# 或者: from typing import AsyncGenerator [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)
示例10: download
# 需要導入模塊: import typing [as 別名]
# 或者: from typing import AsyncGenerator [as 別名]
def download(self, file: TypeLocation, file_size: int,
part_size_kb: Optional[float] = None,
connection_count: Optional[int] = None) -> AsyncGenerator[bytes, None]:
connection_count = connection_count or self._get_connection_count(file_size)
part_size = (part_size_kb or utils.get_appropriated_part_size(file_size)) * 1024
part_count = math.ceil(file_size / part_size)
log.debug("Starting parallel download: "
f"{connection_count} {part_size} {part_count} {file!s}")
await self._init_download(connection_count, file, part_count, part_size)
part = 0
while part < part_count:
tasks = []
for sender in self.senders:
tasks.append(self.loop.create_task(sender.next()))
for task in tasks:
data = await task
if not data:
break
yield data
part += 1
log.trace(f"Part {part} downloaded")
log.debug("Parallel download finished, cleaning up connections")
await self._cleanup()
示例11: consume
# 需要導入模塊: import typing [as 別名]
# 或者: from typing import AsyncGenerator [as 別名]
def consume(
self, listen_for: List[Tuple[str, str]], listener_name: str, **kwargs
) -> AsyncGenerator[List[EventMessage], None]:
"""Consume messages for the given APIs
Examples:
Consuming events::
listen_for = [
('mycompany.auth', 'user_created'),
('mycompany.auth', 'user_updated'),
]
async with event_transport.consume(listen_for) as event_message:
print(event_message)
"""
raise NotImplementedError(
f"Event transport {self.__class__.__name__} does not support listening for events"
)
示例12: get_subscription_games
# 需要導入模塊: import typing [as 別名]
# 或者: from typing import AsyncGenerator [as 別名]
def get_subscription_games(self, subscription_name: str, context: Any) -> AsyncGenerator[
List[SubscriptionGame], None]:
"""Override this method to provide SubscriptionGames for a given subscription.
This method should `yield` a list of SubscriptionGames -> yield [sub_games]
This method will only be used if :meth:`get_subscriptions` has been implemented.
:param context: the value returned from :meth:`prepare_subscription_games_context`
:return a generator object that yields SubscriptionGames
.. code-block:: python
:linenos:
async def get_subscription_games(subscription_name: str, context: Any):
while True:
games_page = await self._get_subscriptions_from_backend(subscription_name, i)
if not games_pages:
yield None
yield [SubGame(game['game_id'], game['game_title']) for game in games_page]
"""
raise NotImplementedError()
示例13: getiter
# 需要導入模塊: import typing [as 別名]
# 或者: from typing import AsyncGenerator [as 別名]
def getiter(
self,
url: str,
url_vars: Dict[str, str] = {},
*,
accept: str = sansio.accept_format(),
jwt: Opt[str] = None,
oauth_token: Opt[str] = None,
) -> AsyncGenerator[Any, None]:
"""Return an async iterable for all the items at a specified endpoint."""
data, more = await self._make_request(
"GET", url, url_vars, b"", accept, jwt=jwt, oauth_token=oauth_token
)
if isinstance(data, dict) and "items" in data:
data = data["items"]
for item in data:
yield item
if more:
# `yield from` is not supported in coroutines.
async for item in self.getiter(
more, url_vars, accept=accept, jwt=jwt, oauth_token=oauth_token
):
yield item
示例14: get_mongodb_user_db
# 需要導入模塊: import typing [as 別名]
# 或者: from typing import AsyncGenerator [as 別名]
def get_mongodb_user_db():
async def _get_mongodb_user_db(
user_model,
) -> AsyncGenerator[MongoDBUserDatabase, None]:
client = motor.motor_asyncio.AsyncIOMotorClient(
"mongodb://localhost:27017",
serverSelectionTimeoutMS=100,
uuidRepresentation="standard",
)
try:
await client.server_info()
except pymongo.errors.ServerSelectionTimeoutError:
pytest.skip("MongoDB not available", allow_module_level=True)
return
db = client["test_database"]
collection = db["users"]
yield MongoDBUserDatabase(user_model, collection)
await collection.drop()
return _get_mongodb_user_db
示例15: sqlalchemy_user_db
# 需要導入模塊: import typing [as 別名]
# 或者: from typing import AsyncGenerator [as 別名]
def sqlalchemy_user_db() -> AsyncGenerator[SQLAlchemyUserDatabase, None]:
Base: DeclarativeMeta = declarative_base()
class User(SQLAlchemyBaseUserTable, Base):
first_name = Column(String, nullable=True)
DATABASE_URL = "sqlite:///./test-sqlalchemy-user.db"
database = Database(DATABASE_URL)
engine = sqlalchemy.create_engine(
DATABASE_URL, connect_args={"check_same_thread": False}
)
Base.metadata.create_all(engine)
await database.connect()
yield SQLAlchemyUserDatabase(UserDB, database, User.__table__)
Base.metadata.drop_all(engine)