本文整理匯總了Python中asyncio.FIRST_EXCEPTION屬性的典型用法代碼示例。如果您正苦於以下問題:Python asyncio.FIRST_EXCEPTION屬性的具體用法?Python asyncio.FIRST_EXCEPTION怎麽用?Python asyncio.FIRST_EXCEPTION使用的例子?那麽, 這裏精選的屬性代碼示例或許可以為您提供幫助。您也可以進一步了解該屬性所在類asyncio
的用法示例。
在下文中一共展示了asyncio.FIRST_EXCEPTION屬性的10個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。
示例1: listen_message_stream
# 需要導入模塊: import asyncio [as 別名]
# 或者: from asyncio import FIRST_EXCEPTION [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()
示例2: deliver_message
# 需要導入模塊: import asyncio [as 別名]
# 或者: from asyncio import FIRST_EXCEPTION [as 別名]
def deliver_message(self, timeout=None):
"""
Deliver next received message.
Deliver next message received from the broker. If no message is available, this methods waits until next message arrives or ``timeout`` occurs.
This method is a *coroutine*.
:param timeout: maximum number of seconds to wait before returning. If timeout is not specified or None, there is no limit to the wait time until next message arrives.
:return: instance of :class:`hbmqtt.session.ApplicationMessage` containing received message information flow.
:raises: :class:`asyncio.TimeoutError` if timeout occurs before a message is delivered
"""
deliver_task = ensure_future(self._handler.mqtt_deliver_next_message(), loop=self._loop)
self.client_tasks.append(deliver_task)
self.logger.debug("Waiting message delivery")
done, pending = yield from asyncio.wait([deliver_task], loop=self._loop, return_when=asyncio.FIRST_EXCEPTION, timeout=timeout)
if deliver_task in done:
self.client_tasks.pop()
return deliver_task.result()
else:
#timeout occured before message received
deliver_task.cancel()
raise asyncio.TimeoutError
示例3: run
# 需要導入模塊: import asyncio [as 別名]
# 或者: from asyncio import FIRST_EXCEPTION [as 別名]
def run(self):
"""Run"""
while True:
loop = asyncio.get_event_loop()
loop.set_debug(self.config.DEBUG)
self.loop = loop
loop.run_until_complete(
asyncio.wait(
(self.connect(), self.process(),),
return_when=asyncio.FIRST_EXCEPTION,
)
)
loop.close()
示例4: test_wait_first_exception
# 需要導入模塊: import asyncio [as 別名]
# 或者: from asyncio import FIRST_EXCEPTION [as 別名]
def test_wait_first_exception(self):
def gen():
when = yield
self.assertAlmostEqual(10.0, when)
yield 0
loop = self.new_test_loop(gen)
# first_exception, task already has exception
a = asyncio.Task(asyncio.sleep(10.0, loop=loop), loop=loop)
@asyncio.coroutine
def exc():
raise ZeroDivisionError('err')
b = asyncio.Task(exc(), loop=loop)
task = asyncio.Task(
asyncio.wait([b, a], return_when=asyncio.FIRST_EXCEPTION,
loop=loop),
loop=loop)
done, pending = loop.run_until_complete(task)
self.assertEqual({b}, done)
self.assertEqual({a}, pending)
self.assertAlmostEqual(0, loop.time())
# move forward to close generator
loop.advance_time(10)
loop.run_until_complete(asyncio.wait([a, b], loop=loop))
示例5: test_wait_first_exception_in_wait
# 需要導入模塊: import asyncio [as 別名]
# 或者: from asyncio import FIRST_EXCEPTION [as 別名]
def test_wait_first_exception_in_wait(self):
def gen():
when = yield
self.assertAlmostEqual(10.0, when)
when = yield 0
self.assertAlmostEqual(0.01, when)
yield 0.01
loop = self.new_test_loop(gen)
# first_exception, exception during waiting
a = asyncio.Task(asyncio.sleep(10.0, loop=loop), loop=loop)
@asyncio.coroutine
def exc():
yield from asyncio.sleep(0.01, loop=loop)
raise ZeroDivisionError('err')
b = asyncio.Task(exc(), loop=loop)
task = asyncio.wait([b, a], return_when=asyncio.FIRST_EXCEPTION,
loop=loop)
done, pending = loop.run_until_complete(task)
self.assertEqual({b}, done)
self.assertEqual({a}, pending)
self.assertAlmostEqual(0.01, loop.time())
# move forward to close generator
loop.advance_time(10)
loop.run_until_complete(asyncio.wait([a, b], loop=loop))
示例6: test_connect_invalid_password
# 需要導入模塊: import asyncio [as 別名]
# 或者: from asyncio import FIRST_EXCEPTION [as 別名]
def test_connect_invalid_password(self):
conn_a = ice.Connection(ice_controlling=True)
conn_b = ice.Connection(ice_controlling=False)
# invite
run(conn_a.gather_candidates())
for candidate in conn_a.local_candidates:
conn_b.add_remote_candidate(candidate)
conn_b.add_remote_candidate(None)
conn_b.remote_username = conn_a.local_username
conn_b.remote_password = conn_a.local_password
# accept
run(conn_b.gather_candidates())
for candidate in conn_b.local_candidates:
conn_a.add_remote_candidate(candidate)
conn_a.add_remote_candidate(None)
conn_a.remote_username = conn_b.local_username
conn_a.remote_password = "wrong-password"
# connect
done, pending = run(
asyncio.wait(
[conn_a.connect(), conn_b.connect()],
return_when=asyncio.FIRST_EXCEPTION,
)
)
for task in pending:
task.cancel()
self.assertEqual(len(done), 1)
self.assertTrue(isinstance(done.pop().exception(), ConnectionError))
# close
run(conn_a.close())
run(conn_b.close())
示例7: main
# 需要導入模塊: import asyncio [as 別名]
# 或者: from asyncio import FIRST_EXCEPTION [as 別名]
def main():
"""
Run maintenance tasks in the background.
This should run forever, as a singleton daemon.
"""
await asyncio.wait(
{
queue_fetches_forever(),
delete_expired_sessions_and_workflows_forever(),
delete_stale_inprogress_file_uploads_forever(),
disable_stale_lesson_auto_update_forever(),
},
return_when=asyncio.FIRST_EXCEPTION,
)
示例8: deliver_message
# 需要導入模塊: import asyncio [as 別名]
# 或者: from asyncio import FIRST_EXCEPTION [as 別名]
def deliver_message(self, timeout=None):
"""
Deliver next received message.
Deliver next message received from the broker. If no message is available, this methods waits until next message arrives or ``timeout`` occurs.
This method is a *coroutine*.
:param timeout: maximum number of seconds to wait before returning. If timeout is not specified or None, there is no limit to the wait time until next message arrives.
:return: instance of :class:`hbmqtt.session.ApplicationMessage` containing received message information flow.
:raises: :class:`asyncio.TimeoutError` if timeout occurs before a message is delivered
"""
deliver_task = asyncio.ensure_future(self._handler.mqtt_deliver_next_message(), loop=self._loop)
self.client_tasks.append(deliver_task)
self.logger.debug("Waiting message delivery")
done, pending = yield from asyncio.wait([deliver_task], loop=self._loop, return_when=asyncio.FIRST_EXCEPTION, timeout=timeout)
if self.client_tasks:
self.client_tasks.pop()
if deliver_task in done:
if deliver_task.exception() is not None:
# deliver_task raised an exception, pass it on to our caller
raise deliver_task.exception()
return deliver_task.result()
else:
#timeout occured before message received
deliver_task.cancel()
raise asyncio.TimeoutError
示例9: on_start
# 需要導入模塊: import asyncio [as 別名]
# 或者: from asyncio import FIRST_EXCEPTION [as 別名]
def on_start(bot):
async def channels():
cursor = None
bot.channels.clear()
while True:
result = await bot.api.conversations.list(
cursor=cursor, types='public_channel,private_channel,im',
)
cursor = None
await asyncio.sleep(0.1)
if 'response_metadata' in result.body:
cursor = result.body['response_metadata'].get('next_cursor')
for c in result.body['channels']:
resp = await bot.api.conversations.info(c['id'])
if not resp.body['ok']:
continue
channel = resp.body['channel']
if channel.get('is_channel'):
bot.channels.append(PublicChannel(**channel))
elif channel.get('is_im'):
bot.ims.append(DirectMessageChannel(**channel))
elif channel.get('is_group'):
bot.groups.append(PrivateChannel(**channel))
await asyncio.sleep(0.1)
if not cursor:
break
async def users():
bot.users.clear()
result = await retry(bot.api.users.list, presence=False)
for u in result.body['members']:
bot.users.append(User(**u))
bot.is_ready = False
await asyncio.wait(
(channels(), users(),), return_when=asyncio.FIRST_EXCEPTION,
)
bot.is_ready = True
return True
示例10: test_add_reader_replace
# 需要導入模塊: import asyncio [as 別名]
# 或者: from asyncio import FIRST_EXCEPTION [as 別名]
def test_add_reader_replace(loop, sock_pair):
c_sock, s_sock = sock_pair
callback_invoked = asyncio.Future()
called1 = False
called2 = False
def any_callback():
if not callback_invoked.done():
callback_invoked.set_result(True)
loop.remove_reader(c_sock.fileno())
def callback1():
# the "bad" callback: if this gets invoked, something went wrong
nonlocal called1
called1 = True
any_callback()
def callback2():
# the "good" callback: this is the one which should get called
nonlocal called2
called2 = True
any_callback()
@asyncio.coroutine
def server_coro():
s_reader, s_writer = yield from asyncio.open_connection(
sock=s_sock)
s_writer.write(b"foo")
yield from s_writer.drain()
@asyncio.coroutine
def client_coro():
loop.add_reader(c_sock.fileno(), callback1)
loop.add_reader(c_sock.fileno(), callback2)
yield from callback_invoked
loop.remove_reader(c_sock.fileno())
assert (yield from loop.sock_recv(c_sock, 3)) == b"foo"
client_done = asyncio.ensure_future(client_coro())
server_done = asyncio.ensure_future(server_coro())
both_done = asyncio.wait(
[server_done, client_done],
return_when=asyncio.FIRST_EXCEPTION)
loop.run_until_complete(asyncio.wait_for(both_done, timeout=0.1))
assert not called1
assert called2