Пример #1
0
 def test_wrap_future_cancel(self):
     f1 = concurrent.futures.Future()
     f2 = asyncio.wrap_future(f1, loop=self.loop)
     f2.cancel()
     test_utils.run_briefly(self.loop)
     self.assertTrue(f1.cancelled())
     self.assertTrue(f2.cancelled())
Пример #2
0
def maybe_future(obj):
    """Return an asyncio Future

    Use instead of gen.maybe_future

    For our compatibility, this must accept:

    - asyncio coroutine (gen.maybe_future doesn't work in tornado < 5)
    - tornado coroutine (asyncio.ensure_future doesn't work)
    - scalar (asyncio.ensure_future doesn't work)
    - concurrent.futures.Future (asyncio.ensure_future doesn't work)
    - tornado Future (works both ways)
    - asyncio Future (works both ways)
    """
    if inspect.isawaitable(obj):
        # already awaitable, use ensure_future
        return asyncio.ensure_future(obj)
    elif isinstance(obj, concurrent.futures.Future):
        return asyncio.wrap_future(obj)
    else:
        # could also check for tornado.concurrent.Future
        # but with tornado >= 5 tornado.Future is asyncio.Future
        f = asyncio.Future()
        f.set_result(obj)
        return f
Пример #3
0
	def run_in_executor(self, executor, callback, *args):
		"""Run callback in executor.

		If no executor is provided, the default executor will be used, which defers execution to
		a background thread.
		"""
		self._logger.debug('Running callback {} with args {} in executor'.format(callback, args))
		if isinstance(callback, asyncio.Handle):
			assert not args
			assert not isinstance(callback, asyncio.TimerHandle)
			if callback.cancelled:
				f = asyncio.Future()
				f.set_result(None)
				return f
			callback, args = callback.callback, callback.args

		if executor is None:
			self._logger.debug('Using default executor')
			executor = self.__default_executor

		if executor is None:
			self._logger.debug('Creating default executor')
			executor = self.__default_executor = QThreadExecutor()

		return asyncio.wrap_future(executor.submit(callback, *args))
Пример #4
0
	def run_in_executor(self, executor, func, *args):
		"""
		Arrange for a func to be called in the specified executor.

		The executor argument should be an Executor instance. The default
		executor is used if executor is None.

		Use functools.partial to pass keywords to the *func*.

		@param executor: executor
		@type executor: concurrent.futures.Executor or None
		@param func: a function to call
		@type func: callable
		@return: a Future
		@rtype: asyncio.Future (or compatible)
		"""
		if executor is None:
			executor = self._default_executor
			if executor is None:
				executor = ForkExecutor(loop=self)
				self._default_executor = executor
		future = executor.submit(func, *args)
		if _real_asyncio is not None:
			future = _real_asyncio.wrap_future(future,
				loop=self._asyncio_wrapper)
		return future
Пример #5
0
 def test_wrap_future_use_global_loop(self, m_events):
     def run(arg):
         return (arg, threading.get_ident())
     ex = concurrent.futures.ThreadPoolExecutor(1)
     f1 = ex.submit(run, 'oi')
     f2 = asyncio.wrap_future(f1)
     self.assertIs(m_events.get_event_loop.return_value, f2._loop)
Пример #6
0
    def get_configuration_async(self, params):
        """Calls `get_configuration` method but designed to use with coroutines

        Args:
            params(dict): ConfigurationParams from lsp specs
        Returns:
            asyncio.Future that can be awaited
        """
        return asyncio.wrap_future(self.get_configuration(params, None))
Пример #7
0
 def test_wrap_future_cancel2(self):
     f1 = concurrent.futures.Future()
     f2 = asyncio.wrap_future(f1, loop=self.loop)
     f1.set_result(42)
     f2.cancel()
     test_utils.run_briefly(self.loop)
     self.assertFalse(f1.cancelled())
     self.assertEqual(f1.result(), 42)
     self.assertTrue(f2.cancelled())
Пример #8
0
 def test_wrap_future_use_global_loop(self):
     with mock.patch('asyncio.futures.events') as events:
         events.get_event_loop = lambda: self.loop
         def run(arg):
             return (arg, threading.get_ident())
         ex = concurrent.futures.ThreadPoolExecutor(1)
         f1 = ex.submit(run, 'oi')
         f2 = asyncio.wrap_future(f1)
         self.assertIs(self.loop, f2._loop)
Пример #9
0
 def run_proc_cmd(self, cmd, *args):
     """Run a command in the p-roc thread and return a future."""
     if self.debug:
         self.debug_log("Calling P-Roc cmd: %s (%s)", cmd, args)
     future = asyncio.wrap_future(
         asyncio.run_coroutine_threadsafe(self.proc_process.run_command(cmd, *args), self.proc_process_instance),
         loop=self.machine.clock.loop)
     future.add_done_callback(self._done)
     return future
 def test_wrap_future_use_global_loop(self):
     with mock.patch('asyncio.futures.events') as events:
         events.get_event_loop = lambda: self.loop
         def run(arg):
             return (arg, threading.get_ident())
         ex = concurrent.futures.ThreadPoolExecutor(1)
         f1 = ex.submit(run, 'oi')
         f2 = asyncio.wrap_future(f1)
         self.assertIs(self.loop, f2._loop)
Пример #11
0
def test_future_wrap_future_cancel2(loop, run_briefly):
    f1 = concurrent.futures.Future()
    f2 = asyncio.wrap_future(f1, loop=loop)
    f1.set_result(42)
    f2.cancel()
    run_briefly(loop)
    assert not f1.cancelled()
    assert f1.result() == 42
    assert f2.cancelled()
Пример #12
0
 def test_wrap_future_cancel2(self):
     f1 = concurrent.futures.Future()
     f2 = asyncio.wrap_future(f1, loop=self.loop)
     f1.set_result(42)
     f2.cancel()
     test_utils.run_briefly(self.loop)
     self.assertFalse(f1.cancelled())
     self.assertEqual(f1.result(), 42)
     self.assertTrue(f2.cancelled())
Пример #13
0
 async def publish(self, message, routing_key='',
                   mandatory=False, immediate=False):
     ch = self._channel._channel
     fut = ch.send_BasicPublish(message, self.name, routing_key=routing_key,
                                mandatory=mandatory, immediate=immediate)
     self._channel._transport.write(ch.data_to_send())
     await self._channel._protocol.drain()
     if fut is not None:
         fut = asyncio.wrap_future(fut, loop=self._channel._protocol._loop)
         return await fut
Пример #14
0
async def main(executor):
    loop = asyncio.get_running_loop()
    teken = []
    for thread in range(THREADSC):
        teken.append(
            asyncio.wrap_future(
                asyncio.run_coroutine_threadsafe(instance(thread), loop)))
    while not teken[len(teken) - 1].done() or len(teken) == 0:
        await asyncio.sleep(1)
        print("waitforit")
Пример #15
0
async def run_in_executor(func, *, executor=None, loop=None):
    executor = executor or ThreadPoolExecutor(max_workers=4)
    loop = loop or asyncio.get_event_loop()

    future = executor.submit(func)
    future = asyncio.wrap_future(future)

    result = await asyncio.wait_for(future, timeout=None, loop=loop)

    return result
Пример #16
0
 def test_future_wrap_future(self):
     def run(arg):
         return (arg, threading.get_ident())
     ex = concurrent.futures.ThreadPoolExecutor(1)
     f1 = ex.submit(run, 'oi')
     f2 = asyncio.wrap_future(f1, loop=self.loop)
     res, ident = self.loop.run_until_complete(f2)
     self.assertIsInstance(f2, asyncio.Future)
     self.assertEqual(res, 'oi')
     self.assertNotEqual(ident, threading.get_ident())
Пример #17
0
        async def do_pause():
            calc_node = self.runner.submit(test_processes.WaitProcess)
            while calc_node.process_state != ProcessState.WAITING:
                await asyncio.sleep(0.1)

            self.assertFalse(calc_node.paused)

            pause_future = controller.pause_process(calc_node.pk)
            future = await with_timeout(asyncio.wrap_future(pause_future))
            result = await self.wait_future(asyncio.wrap_future(future))
            self.assertTrue(result)
            self.assertTrue(calc_node.paused)

            kill_message = 'Sorry, you have to go mate'
            kill_future = controller.kill_process(calc_node.pk,
                                                  msg=kill_message)
            future = await with_timeout(asyncio.wrap_future(kill_future))
            result = await self.wait_future(asyncio.wrap_future(future))
            self.assertTrue(result)
Пример #18
0
def submit_task(completeName, email_body):
    """
    submit the task of writing html file
    :param completeName: 
    :param email_body: 
    :return: 
    """
    with futures.ThreadPoolExecutor(max_workers=3) as executor:
        job = executor.submit(write_files, completeName, email_body)
        return (yield from asyncio.wrap_future(job))
Пример #19
0
async def async_run_in_loop(co, loop, cancel_connect=True):
    our_loop = get_running_loop()
    if loop == our_loop:
        # shortcuts in same loop
        fu = asyncio.ensure_future(co)
    else:
        fu = asyncio.wrap_future(asyncio.run_coroutine_threadsafe(co, loop=loop))
    if not cancel_connect:
        fu = asyncio.shield(fu)
    return await fu
Пример #20
0
 async def wait(self):
     # await a future
     future = asyncio.run_coroutine_threadsafe(super().wait(),
                                               self._event_loop)
     future = asyncio.wrap_future(future)
     done, pending = await asyncio.wait({future})
     if not done:
         raise InternalError(
             "Some internal asyncio error during waiting asyncio.Event.wait"
         )
Пример #21
0
    def kill(
            self,
            msg: Union[str,
                       None] = None) -> Union[bool, plumpy.futures.Future]:
        """
        Kill the process and all the children calculations it called

        :param msg: message
        """
        self.node.logger.info(f'Request to kill Process<{self.node.pk}>')

        had_been_terminated = self.has_terminated()

        result = super().kill(msg)

        # Only kill children if we could be killed ourselves
        if result is not False and not had_been_terminated:
            killing = []
            for child in self.node.called:
                if self.runner.controller is None:
                    self.logger.info(
                        'no controller available to kill child<%s>', child.pk)
                    continue
                try:
                    result = self.runner.controller.kill_process(
                        child.pk, f'Killed by parent<{self.node.pk}>')
                    result = asyncio.wrap_future(
                        result)  # type: ignore[arg-type]
                    if asyncio.isfuture(result):
                        killing.append(result)
                except ConnectionClosed:
                    self.logger.info(
                        'no connection available to kill child<%s>', child.pk)
                except UnroutableError:
                    self.logger.info(
                        'kill signal was unable to reach child<%s>', child.pk)

            if asyncio.isfuture(result):
                # We ourselves are waiting to be killed so add it to the list
                killing.append(result)  # type: ignore[arg-type]

            if killing:
                # We are waiting for things to be killed, so return the 'gathered' future
                kill_future = plumpy.futures.gather(*killing)
                result = self.loop.create_future()

                def done(done_future: plumpy.futures.Future):
                    is_all_killed = all(done_future.result())
                    result.set_result(
                        is_all_killed)  # type: ignore[union-attr]

                kill_future.add_done_callback(done)

        return result
Пример #22
0
    def execute_fetchall(
            self,
            sql: str,
            parameters: Iterable = None) -> Awaitable[Iterable[sqlite3.Row]]:
        parameters = parameters if parameters is not None else []

        def __fetchall(conn: sqlite3.Connection, *args, **kwargs):
            return conn.execute(*args, **kwargs).fetchall()

        return wrap_future(
            self.executor.submit(__fetchall, self.connection, sql, parameters))
Пример #23
0
    async def test_fetchall_prevents_sqlite_misuse(self):
        # test that calling fetchall sufficiently avoids the race
        attempts = 0

        def executemany_fetchall(query, params):
            self.db.executemany(query, params).fetchall()

        while attempts < self.max_misuse_attempts:
            f1 = asyncio.wrap_future(
                self.loop.run_in_executor(
                    self.executor, executemany_fetchall,
                    "update test1 set val='derp' where id=?",
                    ((str(i), ) for i in range(2))))
            f2 = asyncio.wrap_future(
                self.loop.run_in_executor(
                    self.executor, executemany_fetchall,
                    "update test2 set val='derp' where id=?",
                    ((str(i), ) for i in range(2))))
            attempts += 1
            await asyncio.gather(f1, f2)
Пример #24
0
def future_to_aio_future(
    f: AnyFuture, loop: asyncio.AbstractEventLoop
) -> asyncio.Future:
    if isinstance(f, grpc.Future):
        return future_to_aio_future(grpc_future_to_concurrent_future(f), loop)
    elif isinstance(f, asyncio.Future):
        return f
    else:
        # HACK: This method definitely supports a `loop` argument, but mypy
        # doesn't think so.
        return asyncio.wrap_future(f, loop=loop)  # type: ignore
Пример #25
0
    def register_capability_async(self, params: RegistrationParams) -> asyncio.Future:
        """Register a new capability on the client.

        Args:
            params(RegistrationParams): RegistrationParams from lsp specs

        Returns:
            asyncio.Future object that will be resolved once a
            response has been received
        """
        return asyncio.wrap_future(self.register_capability(params, None))
Пример #26
0
 async def run(self):
     while self._run:
         try:
             if await self._backend.poll():
                 job = await self._backend.recv(unpickle=self._unpickle)
                 self._log.debug("Received job: %s", job)
                 f = self._workers.route(job)
                 fut = asyncio.wrap_future(f)
                 await self._backend.send(await fut)
         except asyncio.CancelledError:
             pass
Пример #27
0
    def _poll_events(self):
        poll_sleep = 1 / self.machine.config['mpf']['default_platform_hz']
        while True:
            events = yield from asyncio.wrap_future(
                asyncio.run_coroutine_threadsafe(self.proc_process.read_events_and_watchdog(poll_sleep),
                                                 self.proc_process_instance),
                loop=self.machine.clock.loop)
            if events:
                self.process_events(events)

            yield from asyncio.sleep(poll_sleep, loop=self.machine.clock.loop)
Пример #28
0
    async def run(self):
        # change format to GrayS with bitdepth 32 for descale
        src = self.src[self.frame]
        matrix_s = '709' if src.format.color_family == vapoursynth.RGB else None
        src_luma32 = core.resize.Point(src, format=vapoursynth.YUV444PS, matrix_s=matrix_s)
        src_luma32 = core.std.ShufflePlanes(src_luma32, 0, vapoursynth.GRAY)
        src_luma32 = core.std.Cache(src_luma32)

        # descale each individual frame
        resizer = descale_approx if self.approx else descale_accurate
        clip_list = []
        for h in range(self.min_h, self.max_h + 1):
            clip_list.append(resizer(src_luma32, self.getw(h), h, self.kernel, self.b, self.c, self.taps))
        full_clip = core.std.Splice(clip_list, mismatch=True)
        full_clip = upscale(full_clip, self.getw(src.height), src.height, self.kernel, self.b, self.c, self.taps)
        if self.ar != src.width / src.height:
            src_luma32 = upscale(src_luma32, self.getw(src.height), src.height, self.kernel, self.b, self.c, self.taps)
        expr_full = core.std.Expr([src_luma32 * full_clip.num_frames, full_clip], 'x y - abs dup 0.015 > swap 0 ?')
        full_clip = core.std.CropRel(expr_full, 5, 5, 5, 5)
        full_clip = core.std.PlaneStats(full_clip)
        full_clip = core.std.Cache(full_clip)

        tasks_pending = set()
        futures = {}
        vals = []
        full_clip_len = len(full_clip)
        for frame_index in range(len(full_clip)):
            print(f"{frame_index}/{full_clip_len}", end="\r")
            fut = asyncio.ensure_future(asyncio.wrap_future(full_clip.get_frame_async(frame_index)))
            tasks_pending.add(fut)
            futures[fut] = frame_index
            while len(tasks_pending) >= core.num_threads * (2 if self.approx else 1) + 2:
                tasks_done, tasks_pending = await asyncio.wait(tasks_pending, return_when=asyncio.FIRST_COMPLETED)
                vals += [(futures.pop(task), task.result().props.PlaneStatsAverage) for task in tasks_done]

        tasks_done, _ = await asyncio.wait(tasks_pending)
        vals += [(futures.pop(task), task.result().props.PlaneStatsAverage) for task in tasks_done]
        vals = [v for _, v in sorted(vals)]
        ratios, vals, best_value = self.analyze_results(vals)
        if not os.path.isdir(output_dir):
            os.mkdir(output_dir)

        self.save_plot(vals)
        if self.img_out:
            self.save_images(src_luma32)

        self.txt_output += 'Raw data:\nResolution\t | Relative Error\t | Relative difference from last\n'
        for i, error in enumerate(vals):
            self.txt_output += f'{i + self.min_h:4d}\t\t | {error:.10f}\t\t\t | {ratios[i]:.2f}\n'

        with open(f"{output_dir}/{self.filename}.txt", "w") as file_open:
            file_open.writelines(self.txt_output)

        return best_value
Пример #29
0
 def __init__(self, taskdef: TaskDefinition, cluster):
     kwargs = taskdef.serialize()
     super().__init__(**kwargs)
     self.conn = None
     self.nonce = 0
     self.cluster = cluster
     self.future = Future()
     self.awaitable = asyncio.wrap_future(self.future)
     self.status = WAIT
     self.error = None
     self.result = None
Пример #30
0
    def show_document_async(self, params: ShowDocumentParams) -> asyncio.Future:
        """Display a particular document in the user interface.

        Args:
            params(ShowDocumentParams): ShowDocumentParams from lsp specs

        Returns:
            asyncio.Future object that will be resolved once a
            response has been received
        """
        return asyncio.wrap_future(self.show_document(params, None))
Пример #31
0
 async def _subscriber(self, account_id, instruments=None):
     cancelled = False
     retry_count = 0
     while instruments is None:
         try:
             self.debug("未指定订阅合约,主动请求账户[%s]的合约数据", account_id)
             fut = self.api.qry_instruments(account_id=account_id,
                                            block=False,
                                            push=False)
             async_fut = asyncio.wrap_future(fut)
             rep = await async_fut
             instruments = [inst.name for inst in rep.instruments]
         except asyncio.CancelledError:
             cancelled = True
             break
         except Exception as e:
             self.api.on_error(e)
     while self.is_running() and not cancelled:
         connected = False
         try:
             url = (self.api.get_stream_host() + PRICE_STREAM_ENDPOINT +
                    "?instruments={instruments}").format(
                        accountID=account_id,
                        instruments="%2C".join(instruments),
                    )
             async for data in fetch_stream(self.api.session, url):
                 if not connected:
                     retry_count = 0
                     connected = True
                     self.debug("账户[%s]Price信道订阅成功,订阅品种数为:%s", account_id,
                                len(instruments))
                 if not data:
                     continue
                 valid = self._on_tick(data.strip(MSG_SEP), account_id)
                 if not valid:
                     break
                 if not self.is_running():
                     break
         except asyncio.CancelledError:
             break
         except (asyncio.TimeoutError,
                 concurrent.futures._base.TimeoutError):
             pass  #TODO: continue without reconnect?
         except Exception as e:
             self.api.on_error(e)
         try:
             retry_count += 1
             retry = 1 << (min(retry_count, 4) - 1)
             self.debug("账户[%s]Price信道断开,%s秒后进行第%s次重连", account_id, retry,
                        retry_count)
             await asyncio.sleep(retry)
         except asyncio.CancelledError:
             break
     self.debug("账户[%s]Price信道的订阅已被取消", account_id)
Пример #32
0
    def test_future_wrap_future(self):
        def run(arg):
            return (arg, threading.get_ident())

        ex = concurrent.futures.ThreadPoolExecutor(1)
        f1 = ex.submit(run, 'oi')
        f2 = asyncio.wrap_future(f1, loop=self.loop)
        res, ident = self.loop.run_until_complete(f2)
        self.assertIsInstance(f2, asyncio.Future)
        self.assertEqual(res, 'oi')
        self.assertNotEqual(ident, threading.get_ident())
Пример #33
0
def test_future_wrap_future(loop):
    def run(arg):
        return (arg, threading.get_ident())

    ex = concurrent.futures.ThreadPoolExecutor(1)
    f1 = ex.submit(run, 'oi')
    f2 = asyncio.wrap_future(f1, loop=loop)
    res, ident = loop.run_until_complete(f2)
    assert asyncio.isfuture(f2)
    assert res == 'oi'
    assert ident != threading.get_ident()
Пример #34
0
 async def _run_function_async(self, coro, interface):
     coro = wrap_coro_exception(coro)
     coro = self._wrap_check_async_leakage(coro)
     current_loop = self._get_running_loop()
     loop = self._get_loop(start=True)
     if loop == current_loop:
         value = await coro
     else:
         c_fut = asyncio.run_coroutine_threadsafe(coro, loop)
         a_fut = asyncio.wrap_future(c_fut)
         value = await a_fut
     return self._translate_out(value, interface)
Пример #35
0
    async def _get_pods(self):
        """Get information about build and user pods"""
        app_log.info("Getting pod statistics")
        k8s = self.settings["kubernetes_client"]
        pool = self.settings["executor"]

        get_user_pods = asyncio.wrap_future(
            pool.submit(
                k8s.list_namespaced_pod,
                self.settings["build_namespace"],
                label_selector="app=jupyterhub,component=singleuser-server",
            ))

        get_build_pods = asyncio.wrap_future(
            pool.submit(
                k8s.list_namespaced_pod,
                self.settings["build_namespace"],
                label_selector="component=binderhub-build",
            ))

        return await asyncio.gather(get_user_pods, get_build_pods)
Пример #36
0
    def test_wrap_future(self):

        def run(arg):
            return (arg, threading.get_ident())
        ex = concurrent.futures.ThreadPoolExecutor(1)
        f1 = ex.submit(run, 'oi')
        f2 = asyncio.wrap_future(f1, loop=self.loop)
        res, ident = self.loop.run_until_complete(f2)
        self.assertTrue(asyncio.isfuture(f2))
        self.assertEqual(res, 'oi')
        self.assertNotEqual(ident, threading.get_ident())
        ex.shutdown(wait=True)
Пример #37
0
 def get_chunk(self) -> Awaitable[bytes]:
     with self._chunk_lock:
         future: Future[bytes] = Future()
         # TODO: update backpressure window
         if self._received_chunks:
             chunk = self._received_chunks.pop(0)
             future.set_result(chunk)
         elif self._stream.completion_future.done():
             future.set_result(b"")
         else:
             self._chunk_futures.append(future)
         return asyncio.wrap_future(future)
Пример #38
0
    def test_wrap_future(self):
        def run(arg):
            return (arg, threading.get_ident())

        ex = concurrent.futures.ThreadPoolExecutor(1)
        f1 = ex.submit(run, 'oi')
        f2 = asyncio.wrap_future(f1, loop=self.loop)
        res, ident = self.loop.run_until_complete(f2)
        self.assertTrue(asyncio.isfuture(f2))
        self.assertEqual(res, 'oi')
        self.assertNotEqual(ident, threading.get_ident())
        ex.shutdown(wait=True)
Пример #39
0
    def unregister_capability_async(self, params: UnregistrationParams) -> asyncio.Future:
        """Unregister a new capability on the client.

        Args:
            params(UnregistrationParams): UnregistrationParams from lsp specs
            callback(callable): Callabe which will be called after
                                response from the client is received
        Returns:
            asyncio.Future object that will be resolved once a
            response has been received
        """
        return asyncio.wrap_future(self.unregister_capability(params, None))
Пример #40
0
    def send_request_async(self, method, params=None):
        """Calls `send_request` and wraps `concurrent.futures.Future` with
        `asyncio.Future` so it can be used with `await` keyword.

        Args:
            method(str): The method name of the message to send
            params(any): The payload of the message

        Returns:
            `asyncio.Future` that can be awaited
        """
        return asyncio.wrap_future(self.send_request(method, params))
Пример #41
0
async def evieecutor(func, executor=None, loop=None, *args, **kwargs):
    if not executor:
        executor = concurrent.futures.ThreadPoolExecutor(max_workers=4)

    future = executor.submit(func, *args, **kwargs)
    future = asyncio.wrap_future(future)

    result = await asyncio.wait_for(future,
                                    timeout=None,
                                    loop=loop or asyncio.get_event_loop())
    executor.shutdown(wait=False)

    return result
Пример #42
0
 def shutdown(self):
     """
         Shut down the proxy. This method is thread-safe.
     """
     if not self.should_exit.is_set():
         self.should_exit.set()
         ret = asyncio.run_coroutine_threadsafe(self._shutdown(), loop=self.channel.loop)
         # Weird band-aid to make sure that self._shutdown() is actually executed,
         # which otherwise hangs the process as the proxy server is threaded.
         # This all needs to be simplified when the proxy server runs on asyncio as well.
         if not self.channel.loop.is_running():  # pragma: no cover
             try:
                 self.channel.loop.run_until_complete(asyncio.wrap_future(ret))
             except RuntimeError:
                 pass  # Event loop stopped before Future completed.
Пример #43
0
def maybe_future(obj):
    """Like tornado's deprecated gen.maybe_future

    but more compatible with asyncio for recent versions
    of tornado
    """
    if inspect.isawaitable(obj):
        return asyncio.ensure_future(obj)
    elif isinstance(obj, concurrent.futures.Future):
        return asyncio.wrap_future(obj)
    else:
        # not awaitable, wrap scalar in future
        f = asyncio.Future()
        f.set_result(obj)
        return f
Пример #44
0
def maybe_future(obj):
    """Return an asyncio Future

    Use instead of gen.maybe_future

    For our compatibility, this must accept:

    - asyncio coroutine (gen.maybe_future doesn't work in tornado < 5)
    - tornado coroutine (asyncio.ensure_future doesn't work)
    - scalar (asyncio.ensure_future doesn't work)
    - concurrent.futures.Future (asyncio.ensure_future doesn't work)
    - tornado Future (works both ways)
    - asyncio Future (works both ways)
    """
    if inspect.isawaitable(obj):
        # already awaitable, use ensure_future
        return asyncio.ensure_future(obj)
    elif isinstance(obj, concurrent.futures.Future):
        return asyncio.wrap_future(obj)
    else:
        return to_asyncio_future(gen.maybe_future(obj))
Пример #45
0
 async def put(self, item):
     while True:
         fut = self._put(item)
         if not fut:
             break
         await asyncio.wait_for(asyncio.wrap_future(fut), None)
Пример #46
0
 def __getattr__(self, name):
     requests_method = getattr(requests, name)
     return lambda *args, **kwargs: asyncio.wrap_future(
         self.executor.submit(requests_method, *args, **kwargs)
     )
Пример #47
0
 def test_wrap_future_future(self):
     f1 = asyncio.Future(loop=self.loop)
     f2 = asyncio.wrap_future(f1)
     self.assertIs(f1, f2)
Пример #48
0
 def test_future_wrap_future_future(self):
     f1 = self.create_future()
     f2 = asyncio.wrap_future(f1)
     self.assertIs(f1, f2)
Пример #49
0
 def run(self, fn, *args, **kwargs):
     return asyncio.wrap_future(self.executor.submit(fn, *args, **kwargs))
Пример #50
0
 def __init__(self):
     self.executor = ThreadPoolExecutor(1)
     real_submit = self.executor.submit
     self.executor.submit = lambda *args, **kwargs: asyncio.wrap_future(
         real_submit(*args, **kwargs)
     )
Пример #51
0
 async def get(self):
     item, fut = self._get()
     if fut:
         await asyncio.wait_for(asyncio.wrap_future(fut), None)
         item = fut.result()
     return item
Пример #52
0
 def test_wrap_future(self):
     f = DuckFuture()
     g = asyncio.wrap_future(f)
     assert g is f