Python asyncio.FIRST_EXCEPTION Examples

The following are 16 code examples of asyncio.FIRST_EXCEPTION(). You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may also want to check out all available functions/classes of the module asyncio , or try the search function .
Example #1
Source File: client.py    From jarvis with GNU General Public License v2.0 6 votes vote down vote up
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 
Example #2
Source File: bot.py    From yui with GNU Affero General Public License v3.0 5 votes vote down vote up
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() 
Example #3
Source File: test_tasks.py    From Fluid-Designer with GNU General Public License v3.0 5 votes vote down vote up
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)) 
Example #4
Source File: test_tasks.py    From Fluid-Designer with GNU General Public License v3.0 5 votes vote down vote up
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)) 
Example #5
Source File: test_ice.py    From aioice with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
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()) 
Example #6
Source File: test_tasks.py    From ironpython3 with Apache License 2.0 5 votes vote down vote up
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)) 
Example #7
Source File: test_tasks.py    From ironpython3 with Apache License 2.0 5 votes vote down vote up
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)) 
Example #8
Source File: test_tasks.py    From annotated-py-projects with MIT License 5 votes vote down vote up
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)) 
Example #9
Source File: test_tasks.py    From annotated-py-projects with MIT License 5 votes vote down vote up
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)) 
Example #10
Source File: main.py    From cjworkbench with GNU Affero General Public License v3.0 5 votes vote down vote up
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,
    ) 
Example #11
Source File: test_tasks.py    From Project-New-Reign---Nemesis-Main with GNU General Public License v3.0 5 votes vote down vote up
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)) 
Example #12
Source File: test_tasks.py    From Project-New-Reign---Nemesis-Main with GNU General Public License v3.0 5 votes vote down vote up
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)) 
Example #13
Source File: client.py    From hbmqtt with MIT License 5 votes vote down vote up
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 
Example #14
Source File: gitter.py    From fishroom with GNU General Public License v3.0 4 votes vote down vote up
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() 
Example #15
Source File: core.py    From yui with GNU Affero General Public License v3.0 4 votes vote down vote up
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 
Example #16
Source File: test_qeventloop.py    From asyncqt with BSD 2-Clause "Simplified" License 4 votes vote down vote up
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