コード例 #1
0
ファイル: base.py プロジェクト: mfreshmen/Medusa
    def async_call(self, *args, **kwargs):
        """Call the actual HTTP method asynchronously."""
        content = self._check_authentication()
        if content is not None:
            self.finish(content)
            return

        # Authentication check passed, run the method in a thread
        if PY2:
            # On Python 2, the original exception stack trace is not passed from the executor.
            # This is a workaround based on https://stackoverflow.com/a/27413025/7597273
            tornado_future = TornadoFuture()

            def wrapper():
                try:
                    result = method(*args, **kwargs)
                except:  # noqa: E722 [do not use bare 'except']
                    tornado_future.set_exc_info(sys.exc_info())
                else:
                    tornado_future.set_result(result)

            # `executor.submit()` returns a `concurrent.futures.Future`; wait for it to finish, but ignore the result
            yield executor.submit(wrapper)
            # When this future is yielded, any stored exceptions are raised (with the correct stack trace).
            content = yield tornado_future
        else:
            # On Python 3+, exceptions contain their original stack trace.
            prepared = partial(method, *args, **kwargs)
            content = yield IOLoop.current().run_in_executor(
                executor, prepared)

        self.finish(content)
コード例 #2
0
ファイル: utils.py プロジェクト: Pilarbrist/appscale
 def get_read_version(self, tr):
     tornado_future = TornadoFuture()
     callback = lambda fdb_future: self._handle_fdb_result(
         fdb_future, tornado_future)
     get_future = tr.get_read_version()
     get_future.on_ready(callback)
     return tornado_future
コード例 #3
0
ファイル: utils.py プロジェクト: Pilarbrist/appscale
    def get_range(self,
                  tr,
                  key_slice,
                  limit=0,
                  streaming_mode=fdb.StreamingMode.iterator,
                  iteration=1,
                  reverse=False,
                  snapshot=False):
        tx_reader = tr
        if snapshot:
            tx_reader = tr.snapshot

        begin = key_slice.start
        if not isinstance(begin, fdb.KeySelector):
            begin = fdb.KeySelector.first_greater_or_equal(begin)

        end = key_slice.stop
        if not isinstance(end, fdb.KeySelector):
            end = fdb.KeySelector.first_greater_or_equal(end)

        tornado_future = TornadoFuture()
        callback = lambda fdb_future: self._handle_fdb_result(
            fdb_future, tornado_future)

        get_future = tx_reader._get_range(begin, end, limit, streaming_mode,
                                          iteration, reverse)

        get_future.on_ready(callback)
        return tornado_future
コード例 #4
0
ファイル: utils.py プロジェクト: Pilarbrist/appscale
    def get(self, tr, key, snapshot=False):
        tx_reader = tr
        if snapshot:
            tx_reader = tr.snapshot

        tornado_future = TornadoFuture()
        callback = lambda fdb_future: self._handle_fdb_result(
            fdb_future, tornado_future)
        get_future = tx_reader.get(key)
        get_future.on_ready(callback)
        return tornado_future
コード例 #5
0
ファイル: utils.py プロジェクト: Pilarbrist/appscale
 def commit(self, tr, convert_exceptions=True):
     tornado_future = TornadoFuture()
     callback = lambda fdb_future: self._handle_fdb_result(
         fdb_future, tornado_future)
     commit_future = tr.commit()
     commit_future.on_ready(callback)
     try:
         yield tornado_future
     except fdb.FDBError as fdb_error:
         if convert_exceptions:
             raise InternalError(fdb_error.description)
         else:
             raise
コード例 #6
0
 def test_await_future(self):
     f = Future()
     tf = TornadoFuture()
     def finish_later():
         time.sleep(0.1)
         f.set_result('future')
         tf.set_result('tornado')
     Thread(target=finish_later).start()
     assert self.client.wait([f, tf])
     assert f.done()
     assert tf.done()
     assert f.result() == 'future'
     assert tf.result() == 'tornado'
コード例 #7
0
ファイル: utils.py プロジェクト: antix192/voila
def maybe_future(obj):
    """Like tornado's gen.maybe_future
    but more compatible with asyncio for recent versions
    of tornado
    """
    if isinstance(obj, TornadoFuture):
        return obj
    elif isawaitable(obj):
        return asyncio.ensure_future(obj)
    elif isinstance(obj, ConcurrentFuture):
        return asyncio.wrap_future(obj)
    else:
        # not awaitable, wrap scalar in future
        f = TornadoFuture()
        f.set_result(obj)
        return f
コード例 #8
0
    def execute(self, query, parameters=None, *args, **kwargs):
        """ Runs a Cassandra query asynchronously.

    Returns:
      A Tornado future.
    """
        tornado_future = TornadoFuture()
        io_loop = IOLoop.current()
        cassandra_future = self._session.execute_async(query, parameters,
                                                       *args, **kwargs)
        cassandra_future.add_callbacks(self._handle_page,
                                       self._handle_failure,
                                       callback_args=(io_loop, tornado_future,
                                                      cassandra_future),
                                       errback_args=(io_loop, tornado_future,
                                                     query))
        return tornado_future
コード例 #9
0
    def execute(self, query, parameters=None, *args, **kwargs):
        """ Runs a Cassandra query asynchronously.

    Returns:
      A Tornado future.
    """
        tornado_future = TornadoFuture()
        io_loop = IOLoop.current()
        cassandra_future = self._session.execute_async(query, parameters,
                                                       *args, **kwargs)

        # This list is passed around in order to collect each page of results.
        results = []
        cassandra_future.add_callbacks(
            self._handle_page,
            self._handle_failure,
            callback_args=(io_loop, tornado_future, cassandra_future, results),
            errback_args=(io_loop, tornado_future, query))
        return tornado_future