本文整理汇总了Python中asyncio.wait方法的典型用法代码示例。如果您正苦于以下问题:Python asyncio.wait方法的具体用法?Python asyncio.wait怎么用?Python asyncio.wait使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类asyncio
的用法示例。
在下文中一共展示了asyncio.wait方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: gather
# 需要导入模块: import asyncio [as 别名]
# 或者: from asyncio import wait [as 别名]
def gather(self, wnid, relative="", include_subset=False):
loop = asyncio.get_event_loop()
session = self.create_session(loop)
folders = []
f = loop.run_until_complete(self.download_images(session, wnid, relative))
folders.append(f)
if include_subset:
wnids = self._get_subsets(wnid)
path = self.file_api.join_relative(relative, f)
downloads = asyncio.wait([self.download_images(session, wnid, path) for wnid in wnids])
done, pending = loop.run_until_complete(downloads)
folders += [d.result() for d in done]
session.close()
return folders
示例2: listen_message_stream
# 需要导入模块: import asyncio [as 别名]
# 或者: from asyncio import wait [as 别名]
def listen_message_stream(self, id_blacklist=None):
id_blacklist = set(id_blacklist or [self.me, ])
loop = asyncio.new_event_loop()
asyncio.set_event_loop(loop)
with aiohttp.ClientSession(loop=loop) as session:
self.aioclient_session = session
tasks = [
asyncio.ensure_future(self.fetch(session, room, id_blacklist))
for room in self.rooms
]
done, _ = loop.run_until_complete(
asyncio.wait(tasks, return_when=asyncio.FIRST_EXCEPTION)
)
for d in done:
if d.exception():
raise d.exception()
示例3: wait_for_reaction_remove
# 需要导入模块: import asyncio [as 别名]
# 或者: from asyncio import wait [as 别名]
def wait_for_reaction_remove(bot, emoji=None, *, user=None,
timeout=None, message=None, check=None):
"""Waits for a reaction to be removed by a user from a message within a time period.
Made to act like other discord.py wait_for_* functions but is not fully implemented.
Because of that, wait_for_reaction_remove(self, emoji: list, user, message, timeout=None)
is a better representation of this function's def
returns the actual event or None if timeout
"""
if not (emoji and message) or isinstance(emoji, str):
raise NotImplementedError("wait_for_reaction_remove(self, emoji, "
"message, user=None, timeout=None, "
"check=None) is a better representation "
"of this function definition")
remove_event = ReactionRemoveEvent(emoji, user, check=check)
_reaction_remove_events[message.id] = remove_event
done, pending = await asyncio.wait([remove_event.wait()],
timeout=timeout)
res = _reaction_remove_events.pop(message.id)
try:
return done.pop().result() and res
except:
return None
示例4: wait_for_first_response
# 需要导入模块: import asyncio [as 别名]
# 或者: from asyncio import wait [as 别名]
def wait_for_first_response(tasks, converters):
"""given a list of unawaited tasks and non-coro result parsers to be called on the results,
this function returns the 1st result that is returned and converted
if it is possible for 2 tasks to complete at the same time,
only the 1st result deteremined by asyncio.wait will be returned
returns None if none successfully complete
returns 1st error raised if any occur (probably)
"""
primed = [wait_for_result(t, c) for t, c in zip(tasks, converters)]
done, pending = await asyncio.wait(primed, return_when=asyncio.FIRST_COMPLETED)
for p in pending:
p.cancel()
try:
return done.pop().result()
except NotImplementedError as e:
raise e
except:
return None
示例5: async_run
# 需要导入模块: import asyncio [as 别名]
# 或者: from asyncio import wait [as 别名]
def async_run(tasks):
"""
run a group of tasks async
Requires the tasks arg to be a list of functools.partial()
"""
if not tasks:
return
# start a new async event loop
loop = asyncio.get_event_loop()
# https://github.com/python/asyncio/issues/258
executor = concurrent.futures.ThreadPoolExecutor(5)
loop.set_default_executor(executor)
async_tasks = [asyncio.ensure_future(async_task(task, loop)) for task in tasks]
# run tasks in parallel
loop.run_until_complete(asyncio.wait(async_tasks))
# deal with errors (exceptions, etc)
for task in async_tasks:
error = task.exception()
if error is not None:
raise error
executor.shutdown(wait=True)
示例6: fetch_bar2
# 需要导入模块: import asyncio [as 别名]
# 或者: from asyncio import wait [as 别名]
def fetch_bar2():
day = datetime.datetime.strptime('20160114', '%Y%m%d').replace(tzinfo=pytz.FixedOffset(480))
end = datetime.datetime.strptime('20160914', '%Y%m%d').replace(tzinfo=pytz.FixedOffset(480))
while day <= end:
day, trading = await is_trading_day(day)
if trading:
print('process ', day)
tasks = [
asyncio.ensure_future(update_from_shfe(day)),
asyncio.ensure_future(update_from_dce(day)),
asyncio.ensure_future(update_from_czce(day)),
asyncio.ensure_future(update_from_cffex(day)),
]
await asyncio.wait(tasks)
day += datetime.timedelta(days=1)
print('all done!')
# asyncio.get_event_loop().run_until_complete(fetch_bar2())
# create_main_all()
# fetch_from_quandl_all()
# clean_dailybar()
# load_kt_data()
示例7: loop
# 需要导入模块: import asyncio [as 别名]
# 或者: from asyncio import wait [as 别名]
def loop(self: 'TelegramClient') -> asyncio.AbstractEventLoop:
"""
Property with the ``asyncio`` event loop used by this client.
Example
.. code-block:: python
# Download media in the background
task = client.loop.create_task(message.download_media())
# Do some work
...
# Join the task (wait for it to complete)
await task
"""
return self._loop
示例8: test_operations
# 需要导入模块: import asyncio [as 别名]
# 或者: from asyncio import wait [as 别名]
def test_operations(pool_or_redis, test_case, pool_size):
repeat = 100
redis = await pool_or_redis(pool_size)
done, pending = await asyncio.wait(
[asyncio.ensure_future(test_case(redis, i))
for i in range(repeat)])
assert not pending
success = 0
failures = []
for fut in done:
exc = fut.exception()
if exc is None:
success += 1
else:
failures.append(exc)
assert repeat == success, failures
assert not failures
示例9: publish
# 需要导入模块: import asyncio [as 别名]
# 或者: from asyncio import wait [as 别名]
def publish(cls, service: Any, data: Any, topic: str, wait: bool = True, message_protocol: Any = MESSAGE_PROTOCOL_DEFAULT, topic_prefix: Optional[str] = MESSAGE_TOPIC_PREFIX, **kwargs: Any) -> None:
message_protocol = getattr(service, 'message_protocol', None) if message_protocol == MESSAGE_PROTOCOL_DEFAULT else message_protocol
payload = data
if message_protocol:
build_message_func = getattr(message_protocol, 'build_message', None)
if build_message_func:
payload = await build_message_func(service, topic, data, **kwargs)
topic_arn = await cls.create_topic(cls, topic, service.context, topic_prefix)
async def _publish_message() -> None:
await cls.publish_message(cls, topic_arn, payload, service.context)
if wait:
await _publish_message()
else:
loop = asyncio.get_event_loop() # type: Any
loop.create_task(_publish_message())
示例10: main
# 需要导入模块: import asyncio [as 别名]
# 或者: from asyncio import wait [as 别名]
def main(connections2observe4ip):
logger = logging.getLogger('asyncio.main')
logger.debug('starting jobs observing connections')
# Starting the clients
jobs_on_connections = []
for _, connection_name, ping_ip in connections2observe4ip:
# ------------------------------------------------------------------
# This front-end code hides all details of connection.
# We just use its name - such name should be meaningful for user.
# like: "main_dns_server", "backup_ntp_server", ...
# Another words, all we want here is stg like:
# "give me connection to main_dns_server"
# ------------------------------------------------------------------
con_logger = logging.getLogger('tcp-async-io.{}'.format(connection_name))
tcp_connection = get_connection(name=connection_name, variant='asyncio', logger=con_logger)
# client_task= asyncio.ensure_future(ping_observing_task(tcp_connection, ping_ip))
jobs_on_connections.append(ping_observing_task(tcp_connection, ping_ip))
# await observers job to be done
completed, pending = await asyncio.wait(jobs_on_connections)
logger.debug('all jobs observing connections are done')
# ==============================================================================
示例11: launch_shards
# 需要导入模块: import asyncio [as 别名]
# 或者: from asyncio import wait [as 别名]
def launch_shards(self):
if self.shard_count is None:
self.shard_count, gateway = await self.http.get_bot_gateway()
else:
gateway = await self.http.get_gateway()
self._connection.shard_count = self.shard_count
shard_ids = self.shard_ids if self.shard_ids else range(self.shard_count)
self._connection.shard_ids = shard_ids
for shard_id in shard_ids:
await self.launch_shard(gateway, shard_id)
shards_to_wait_for = []
for shard in self.shards.values():
shard.complete_pending_reads()
shards_to_wait_for.append(shard.wait())
# wait for all pending tasks to finish
await utils.sane_wait_for(shards_to_wait_for, timeout=300.0)
示例12: close
# 需要导入模块: import asyncio [as 别名]
# 或者: from asyncio import wait [as 别名]
def close(self):
"""|coro|
Closes the connection to Discord.
"""
if self.is_closed():
return
self._closed = True
for vc in self.voice_clients:
try:
await vc.disconnect()
except Exception:
pass
to_close = [asyncio.ensure_future(shard.ws.close(code=1000), loop=self.loop) for shard in self.shards.values()]
if to_close:
await asyncio.wait(to_close)
await self.http.close()
示例13: test_stream_cancel
# 需要导入模块: import asyncio [as 别名]
# 或者: from asyncio import wait [as 别名]
def test_stream_cancel(event_loop):
async def cancel(task):
await asyncio.sleep(0.001)
task.cancel()
async def test_stream_iterations(stream):
async with async_timeout.timeout(0.5):
while True:
await _stream_iteration(stream)
async with aiohttp.ClientSession() as session:
client = peony.client.BasePeonyClient("", "", session=session)
context = peony.stream.StreamResponse(method='GET',
url="http://whatever.com",
client=client)
with context as stream:
with patch.object(stream, '_connect',
side_effect=stream_content):
coro = test_stream_iterations(stream)
task = event_loop.create_task(coro)
cancel_task = event_loop.create_task(cancel(task))
with async_timeout.timeout(1):
await asyncio.wait([task, cancel_task])
示例14: __aexit__
# 需要导入模块: import asyncio [as 别名]
# 或者: from asyncio import wait [as 别名]
def __aexit__(self, exc_type, exc_val, exc_tb):
"""Wait for executors to finish, then return."""
logger.info(
__("Waiting for executor count to drop to 0, now it is {}", self.value)
)
await self.condition.acquire()
try:
await self.condition.wait()
finally:
self.condition.release()
logger.debug(
__(
"Sync semaphore dropped to 0, tag sequence was {}.",
self.tag_sequence,
)
)
self.active = False
return False
示例15: execution_barrier
# 需要导入模块: import asyncio [as 别名]
# 或者: from asyncio import wait [as 别名]
def execution_barrier(self):
"""Wait for executors to finish.
At least one must finish after this point to avoid a deadlock.
"""
async def _barrier():
"""Enter the sync block and exit the app afterwards."""
async with self.sync_counter:
pass
await consumer.exit_consumer()
self._ensure_counter()
await asyncio.wait(
[_barrier(), consumer.run_consumer(),]
)
self.sync_counter = self._SynchronizationManagerDummy()