Python asyncio.coroutine() Examples

The following are 30 code examples of asyncio.coroutine(). 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: utils.py    From quart with MIT License 8 votes vote down vote up
def run_sync(func: Callable[..., Any]) -> Callable[..., Coroutine[Any, None, None]]:
    """Ensure that the sync function is run within the event loop.

    If the *func* is not a coroutine it will be wrapped such that
    it runs in the default executor (use loop.set_default_executor
    to change). This ensures that synchronous functions do not
    block the event loop.
    """

    @wraps(func)
    async def _wrapper(*args: Any, **kwargs: Any) -> Any:
        loop = asyncio.get_running_loop()
        result = await loop.run_in_executor(
            None, copy_context().run, partial(func, *args, **kwargs)
        )
        if isgenerator(result):
            return run_sync_iterable(result)  # type: ignore
        else:
            return result

    _wrapper._quart_async_wrapper = True  # type: ignore
    return _wrapper 
Example #2
Source File: support.py    From molotov with Apache License 2.0 6 votes vote down vote up
def async_test(func):
    @functools.wraps(func)
    def _async_test(*args, **kw):
        cofunc = asyncio.coroutine(func)
        oldloop = asyncio.get_event_loop()
        loop = asyncio.new_event_loop()
        asyncio.set_event_loop(loop)
        loop.set_debug(True)
        console = SharedConsole(interval=0)
        results = SharedCounters(
            "WORKER", "REACHED", "RATIO", "OK", "FAILED", "MINUTE_OK", "MINUTE_FAILED"
        )
        kw["loop"] = loop
        kw["console"] = console
        kw["results"] = results
        try:
            loop.run_until_complete(cofunc(*args, **kw))
        finally:
            loop.stop()
            loop.close()
            asyncio.set_event_loop(oldloop)

    return _async_test 
Example #3
Source File: conversationwidget.py    From qhangups with GNU General Public License v3.0 6 votes vote down vote up
def load_events(self):
        """Load more events for this conversation (coroutine)"""
        # Don't try to load while we're already loading.
        if not self.is_loading and not self.first_loaded:
            logger.debug('Loading more conversation events')

            self.is_loading = True

            try:
                conv_events = yield from self.conv.get_events(self.conv.events[0].id_)
            except (IndexError, hangups.NetworkError):
                conv_events = []

            if conv_events:
                self.scroll_prev_height = self.messagesWebView.page().mainFrame().contentsSize().height()
            else:
                self.first_loaded = True

            for event in reversed(conv_events):
                self.on_event(event, set_title=False, set_unread=False, prepend=True)

            self.is_loading = False 
Example #4
Source File: aiohttp_.py    From uplink with MIT License 6 votes vote down vote up
def create(cls, *args, **kwargs):
        """
        Builds a client instance with
        :py:class:`aiohttp.ClientSession` arguments.

        Instead of directly initializing this class with a
        :py:class:`aiohttp.ClientSession`, use this method to have the
        client lazily construct a session when sending the first
        request. Hence, this method guarantees that the creation of the
        underlying session happens inside of a coroutine.

        Args:
            *args: positional arguments that
                :py:class:`aiohttp.ClientSession` takes.
            **kwargs: keyword arguments that
                :py:class:`aiohttp.ClientSession` takes.
        """
        session_build_args = cls._create_session(*args, **kwargs)
        return AiohttpClient(session=session_build_args) 
Example #5
Source File: test_clients.py    From uplink with MIT License 6 votes vote down vote up
def test_threaded_response(self, mocker):
        # Setup
        import asyncio

        @asyncio.coroutine
        def coroutine():
            return 1

        def not_a_coroutine():
            return 2

        response = mocker.Mock()
        response.coroutine = coroutine
        response.not_coroutine = not_a_coroutine
        threaded_response = aiohttp_.ThreadedResponse(response)

        # Run
        threaded_coroutine = threaded_response.coroutine
        return_value = threaded_coroutine()

        # Verify
        assert isinstance(threaded_coroutine, aiohttp_.ThreadedCoroutine)
        assert return_value == 1
        assert threaded_response.not_coroutine is not_a_coroutine 
Example #6
Source File: test_discovery.py    From pyquarkchain with MIT License 6 votes vote down vote up
def test_wait_ping(echo):
    proto = MockDiscoveryProtocol([])
    node = random_node()

    # Schedule a call to proto.recv_ping() simulating a ping from the node we expect.
    recv_ping_coroutine = asyncio.coroutine(lambda: proto.recv_ping_v4(node, echo, b""))
    asyncio.ensure_future(recv_ping_coroutine())

    got_ping = await proto.wait_ping(node)

    assert got_ping
    # Ensure wait_ping() cleaned up after itself.
    assert node not in proto.ping_callbacks

    # If we waited for a ping from a different node, wait_ping() would timeout and thus return
    # false.
    recv_ping_coroutine = asyncio.coroutine(lambda: proto.recv_ping_v4(node, echo, b""))
    asyncio.ensure_future(recv_ping_coroutine())

    node2 = random_node()
    got_ping = await proto.wait_ping(node2)

    assert not got_ping
    assert node2 not in proto.ping_callbacks 
Example #7
Source File: test_clients.py    From uplink with MIT License 6 votes vote down vote up
def test_wrap_callback(self, mocker):
        import asyncio

        # Setup
        c = AiohttpClient()
        mocker.spy(c, "_sync_callback_adapter")

        # Run: with callback that is not a coroutine
        def callback(*_):
            pass

        c.wrap_callback(callback)

        # Verify: Should wrap it
        c._sync_callback_adapter.assert_called_with(callback)

        # Run: with coroutine callback
        coroutine_callback = asyncio.coroutine(callback)
        assert c.wrap_callback(coroutine_callback) is coroutine_callback 
Example #8
Source File: test_clients.py    From uplink with MIT License 6 votes vote down vote up
def test_request_send(self, mocker, aiohttp_session_mock):
        # Setup
        import asyncio

        expected_response = mocker.Mock()

        @asyncio.coroutine
        def request(*args, **kwargs):
            return expected_response

        aiohttp_session_mock.request = request
        client = aiohttp_.AiohttpClient(aiohttp_session_mock)

        # Run
        response = client.send((1, 2, {}))
        loop = asyncio.get_event_loop()
        value = loop.run_until_complete(asyncio.ensure_future(response))

        # Verify
        assert value == expected_response 
Example #9
Source File: test_retry.py    From uplink with MIT License 6 votes vote down vote up
def test_retry_with_asyncio(mock_client, mock_response):
    import asyncio

    @asyncio.coroutine
    def coroutine():
        return mock_response

    # Setup
    mock_response.with_json({"id": 123, "name": "prkumar"})
    mock_client.with_side_effect([Exception, coroutine()])
    mock_client.with_io(io.AsyncioStrategy())
    github = GitHub(base_url=BASE_URL, client=mock_client)

    # Run
    awaitable = github.get_user("prkumar")
    loop = asyncio.get_event_loop()
    response = loop.run_until_complete(asyncio.ensure_future(awaitable))

    # Verify
    assert len(mock_client.history) == 2
    assert response.json() == {"id": 123, "name": "prkumar"} 
Example #10
Source File: device.py    From Facedancer with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
def emulate(self, *coroutines: Iterable[asyncio.coroutine]):
        """ Convenience method that runs a full method in a blocking manner.
        Performs connect, run, and then disconnect.

        Parameters:
            *coroutines -- any asyncio coroutines to be executed concurrently
                           with our emulation
        """

        self.connect()

        try:
            self.run_with(*coroutines)
        except KeyboardInterrupt:
            pass
        finally:
            self.disconnect()


    #
    # I/O interface.
    # 
Example #11
Source File: test_discovery.py    From pyquarkchain with MIT License 6 votes vote down vote up
def test_bond():
    proto = MockDiscoveryProtocol([])
    node = random_node()

    token = b"token"
    # Do not send pings, instead simply return the pingid we'd expect back together with the pong.
    proto.send_ping_v4 = lambda remote: token

    # Pretend we get a pong from the node we are bonding with.
    proto.wait_pong_v4 = asyncio.coroutine(lambda n, t: t == token and n == node)

    bonded = await proto.bond(node)

    assert bonded

    # If we try to bond with any other nodes we'll timeout and bond() will return False.
    node2 = random_node()
    bonded = await proto.bond(node2)

    assert not bonded 
Example #12
Source File: test_discovery.py    From pyquarkchain with MIT License 6 votes vote down vote up
def test_update_routing_table_triggers_bond_if_eviction_candidate():
    proto = MockDiscoveryProtocol([])
    old_node, new_node = random_node(), random_node()

    bond_called = False

    def bond(node):
        nonlocal bond_called
        bond_called = True
        assert node == old_node

    proto.bond = asyncio.coroutine(bond)
    # Pretend our routing table failed to add the new node by returning the least recently seen
    # node for an eviction check.
    proto.routing.add_node = lambda n: old_node

    proto.update_routing_table(new_node)

    assert new_node not in proto.routing
    # The update_routing_table() call above will have scheduled a future call to proto.bond() so
    # we need to yield here to give it a chance to run.
    await asyncio.sleep(0.001)
    assert bond_called 
Example #13
Source File: stream.py    From peony-twitter with MIT License 6 votes vote down vote up
def _connect(self):
        """
            Connect to the stream

        Returns
        -------
        asyncio.coroutine
            The streaming response
        """
        logger.debug("connecting to the stream")
        await self.client.setup
        if self.session is None:
            self.session = self.client._session
        kwargs = await self.client.headers.prepare_request(**self.kwargs)
        request = self.client.error_handler(self.session.request)

        return await request(timeout=0, **kwargs) 
Example #14
Source File: login.py    From lrrbot with Apache License 2.0 6 votes vote down vote up
def with_minimal_session(func):
	"""
	Pass the current login session information to the function

	Do not include extra session information, intended for master.html. Useful for
	places that need the current user, but shouldn't (or don't need to) call
	botinteract.

	Usage:
	@server.app.route('/path')
	@with_minimal_session
	def handler(session):
		...
	"""
	@functools.wraps(func)
	async def wrapper(*args, **kwargs):
		kwargs['session'] = await load_session(include_url=False, include_header=False)
		return await asyncio.coroutine(func)(*args, **kwargs)
	return wrapper 
Example #15
Source File: interconnect.py    From sawtooth-core with Apache License 2.0 6 votes vote down vote up
def _do_heartbeat(self):
        while True:
            try:
                if self._socket.getsockopt(zmq.TYPE) == zmq.ROUTER:
                    yield from self._do_router_heartbeat()
                elif self._socket.getsockopt(zmq.TYPE) == zmq.DEALER:
                    yield from self._do_dealer_heartbeat()
                yield from asyncio.sleep(self._heartbeat_interval,
                                         loop=self._event_loop)
            except CancelledError:  # pylint: disable=try-except-raise
                # The concurrent.futures.CancelledError is caught by asyncio
                # when the Task associated with the coroutine is cancelled.
                # The raise is required to stop this component.
                raise
            except Exception as e:  # pylint: disable=broad-except
                LOGGER.exception(
                    "An error occurred while sending heartbeat: %s", e) 
Example #16
Source File: login.py    From lrrbot with Apache License 2.0 6 votes vote down vote up
def require_mod(func):
	"""
	Like with_session, but if the user isn't logged in,
	send them via the login screen. If the user isn't
	a moderator, kick them out.
	"""
	@functools.wraps(func)
	async def wrapper(*args, **kwargs):
		session = await load_session()
		if session['user']['id'] is not None:
			kwargs['session'] = session
			if session['user']['is_mod']:
				return await asyncio.coroutine(func)(*args, **kwargs)
			else:
				return flask.render_template('require_mod.html', session=session)
		else:
			return await login(session['url'])
	return wrapper 
Example #17
Source File: test_database.py    From aiocouchdb with BSD 2-Clause "Simplified" License 6 votes vote down vote up
def test_changes_continuous_reading(self):
        ids = [utils.uuid() for _ in range(3)]

        @asyncio.coroutine
        def task():
            for idx in ids:
                yield from self.db[idx].update({})
        asyncio.Task(task())

        with (yield from self.db.changes(feed='continuous',
                                         timeout=1000)) as feed:

            while True:
                self.assertTrue(feed.is_active())
                event = yield from feed.next()
                if event is None:
                    break
                self.assertIsInstance(event, dict)
                self.assertIn(event['id'], ids)

            self.assertFalse(feed.is_active()) 
Example #18
Source File: test_database.py    From aiocouchdb with BSD 2-Clause "Simplified" License 6 votes vote down vote up
def test_changes_eventsource(self):
        ids = [utils.uuid() for _ in range(3)]

        @asyncio.coroutine
        def task():
            for idx in ids:
                yield from self.db[idx].update({})
        asyncio.Task(task())

        with (yield from self.db.changes(feed='eventsource',
                                         timeout=1000)) as feed:

            while True:
                self.assertTrue(feed.is_active())
                event = yield from feed.next()
                if event is None:
                    break
                self.assertIsInstance(event, dict)
                self.assertIn(event['id'], ids) 
Example #19
Source File: conftest.py    From aiohttp_admin with Apache License 2.0 6 votes vote down vote up
def pytest_pyfunc_call(pyfuncitem):
    """
    Run asyncio marked test functions in an event loop instead of a normal
    function call.
    """
    if 'run_loop' in pyfuncitem.keywords:
        funcargs = pyfuncitem.funcargs
        loop = funcargs['loop']
        testargs = {arg: funcargs[arg]
                    for arg in pyfuncitem._fixtureinfo.argnames}

        if not asyncio.iscoroutinefunction(pyfuncitem.obj):
            func = asyncio.coroutine(pyfuncitem.obj)
        else:
            func = pyfuncitem.obj
        loop.run_until_complete(func(**testargs))
        return True 
Example #20
Source File: test_hub.py    From bricknil with Apache License 2.0 6 votes vote down vote up
def test_run_hub(self, data):

        Hub.hubs = []
        sensor_name = 'sensor'
        sensor = data.draw(st.sampled_from(self.sensor_list))
        capabilities = self._draw_capabilities(data, sensor)

        hub_type = data.draw(st.sampled_from(self.hub_list))
        TestHub, stop_evt = self._get_hub_class(hub_type, sensor, sensor_name, capabilities)
        hub = TestHub('test_hub')

        # Start the hub
        #kernel.run(self._emit_control(TestHub))
        with patch('Adafruit_BluefruitLE.get_provider') as ble,\
             patch('bricknil.ble_queue.USE_BLEAK', False) as use_bleak:
            ble.return_value = MockBLE(hub)
            sensor_obj = getattr(hub, sensor_name)
            sensor_obj.send_message = Mock(side_effect=coroutine(lambda x,y: "the awaitable should return this"))
            kernel.run(self._emit_control, data, hub, stop_evt, ble(), sensor_obj)
            #start(system) 
Example #21
Source File: command_parser.py    From lrrbot with Apache License 2.0 5 votes vote down vote up
def add(self, pattern, function):
		if not asyncio.iscoroutinefunction(function):
			function = asyncio.coroutine(function)
		pattern = pattern.replace(" ", r"(?:\s+)")
		self.commands[pattern] = {
			"groups": re.compile(pattern, re.IGNORECASE).groups,
			"func": function,
		}
		self.re_botcommand = None 
Example #22
Source File: interconnect.py    From sawtooth-core with Apache License 2.0 5 votes vote down vote up
def _remove_expired_futures(self):
        while True:
            try:
                yield from asyncio.sleep(self._connection_timeout,
                                         loop=self._event_loop)
                self._futures.remove_expired()
            except CancelledError:  # pylint: disable=try-except-raise
                # The concurrent.futures.CancelledError is caught by asyncio
                # when the Task associated with the coroutine is cancelled.
                # The raise is required to stop this component.
                raise
            except Exception as e:  # pylint: disable=broad-except
                LOGGER.exception("An error occurred while"
                                 " cleaning up expired futures: %s",
                                 e) 
Example #23
Source File: login.py    From lrrbot with Apache License 2.0 5 votes vote down vote up
def require_login(func):
	"""
	Like with_session, but if the user isn't logged in,
	send them via the login screen.
	"""
	@functools.wraps(func)
	async def wrapper(*args, **kwargs):
		session = await load_session()
		if session['user']['id'] is not None:
			kwargs['session'] = session
			return await asyncio.coroutine(func)(*args, **kwargs)
		else:
			return await login(session['url'])
	return wrapper 
Example #24
Source File: authn.py    From aiocouchdb with BSD 2-Clause "Simplified" License 5 votes vote down vote up
def wrap(self, request_func):
        """Wraps request coroutine function to apply the authentication context.
        """
        @functools.wraps(request_func)
        @asyncio.coroutine
        def wrapper(method, url, headers, **kwargs):
            self.apply(url, headers)
            response = yield from request_func(method, url,
                                               headers=headers, **kwargs)
            self.update(response)
            return response
        return wrapper 
Example #25
Source File: test_pydest.py    From pydest with MIT License 5 votes vote down vote up
def test_update_manifest_called(self):
        mock = MagicMock()
        mock.method.return_value = None
        update_manifest_mock = asyncio.coroutine(mock.method)

        destiny = pydest.Pydest('123')
        destiny._manifest.update_manifest = update_manifest_mock
        res = await destiny.update_manifest(language='en')
        await destiny.close()

        mock.method.assert_called_with('en') 
Example #26
Source File: test_pydest.py    From pydest with MIT License 5 votes vote down vote up
def test_decode_hash_called(self):
        mock = MagicMock()
        mock.method.return_value = None
        decode_hash_mock = asyncio.coroutine(mock.method)

        destiny = pydest.Pydest('123')
        destiny._manifest.decode_hash = decode_hash_mock
        res = await destiny.decode_hash(123, 'ActivityDefinition', language='fr')
        await destiny.close()

        mock.method.assert_called_with(123, 'ActivityDefinition', 'fr') 
Example #27
Source File: no_notebook.py    From vpython-jupyter with MIT License 5 votes vote down vote up
def onOpen(self):
        global websocketserving
        websocketserving = True

    # For Python 3.5 and later, the newer syntax eliminates "@asyncio.coroutine"
    # in favor of "async def onMessage...", and "yield from" with "await".
    # Attempting to use the older Python 3.4 syntax was not successful, so this
    # no-notebook version of VPython requires Python 3.5.3 or later.
    # @asyncio.coroutine
    # def onMessage(self, data, isBinary): # data includes canvas update, events, pick, compound
    # data includes canvas update, events, pick, compound 
Example #28
Source File: gui.py    From skan with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def _async(coroutine, *args):
    loop = asyncio.get_event_loop()
    return (yield from loop.run_in_executor(None, coroutine, *args)) 
Example #29
Source File: __init__.py    From aiohttp-jinja2 with Apache License 2.0 5 votes vote down vote up
def template(
    template_name: str,
    *,
    app_key: str = APP_KEY,
    encoding: str = 'utf-8',
    status: int = 200
) -> Any:

    def wrapper(func: Any) -> Any:
        @functools.wraps(func)
        async def wrapped(*args: Any) -> web.StreamResponse:
            if asyncio.iscoroutinefunction(func):
                coro = func
            else:
                warnings.warn("Bare functions are deprecated, "
                              "use async ones", DeprecationWarning)
                coro = asyncio.coroutine(func)
            context = await coro(*args)
            if isinstance(context, web.StreamResponse):
                return context

            # Supports class based views see web.View
            if isinstance(args[0], AbstractView):
                request = args[0].request
            else:
                request = args[-1]

            response = render_template(template_name, request, context,
                                       app_key=app_key, encoding=encoding)
            response.set_status(status)
            return response
        return wrapped
    return wrapper 
Example #30
Source File: test_server.py    From aiocouchdb with BSD 2-Clause "Simplified" License 5 votes vote down vote up
def trigger_db_update(self, db):
        @asyncio.coroutine
        def task():
            yield from asyncio.sleep(0.1)
            yield from db[utils.uuid()].update({})
        asyncio.Task(task())