我们从Python开源项目中,提取了以下50个代码示例,用于说明如何使用concurrent.futures.Future()。
def test_register_failure(mocker, SETTINGS, WORKERS): SETTINGS.GITHUB_SECRET = 'notasecret' from concurrent.futures import Future future = Future() future.set_exception(Exception()) register_webhook = mocker.patch( 'jenkins_epo.main.register_webhook', CoroutineMock(return_value=[future]), ) mocker.patch('jenkins_epo.main.WORKERS', WORKERS) from jenkins_epo.main import register with pytest.raises(SystemExit): yield from register() assert WORKERS.start.mock_calls assert register_webhook.mock_calls assert WORKERS.terminate.mock_calls
def set_exc_info(self, exc_info): """Sets the exception information of a ``Future.`` Preserves tracebacks on Python 2. .. versionadded:: 4.0 """ self._exc_info = exc_info self._log_traceback = True if not _GC_CYCLE_FINALIZERS: self._tb_logger = _TracebackLogger(exc_info) try: self._set_done() finally: # Activate the logger after all callbacks have had a # chance to call result() or exception(). if self._log_traceback and self._tb_logger is not None: self._tb_logger.activate() self._exc_info = exc_info
def _wait_for_done(self, timeout): """ Will not return until either timeout expires or future becomes "done". There is one potential deadlock situation here: The deadlock occurs if we await_result while at the same time, this future needs to await_result from another future ---> To be safe, don't use await_result() in a Qt slot... """ if self.cancelled(): raise CancelledError("Future was cancelled") # pragma: no-cover if not self.done(): self.timer_timeout = None if (timeout is not None) and timeout > 0: self._timer_timeout = MainThreadTimer(timeout*1000) self._timer_timeout.timeout.connect(self._exit_loop) self._timer_timeout.start() self.loop = QtCore.QEventLoop() self.add_done_callback(self._exit_loop) self.loop.exec_() if self._timer_timeout is not None: if not self._timer_timeout.isActive(): return TimeoutError("Timeout occured") # pragma: no-cover else: self._timer_timeout.stop()
def message_send(self, to, msg): """ Transmit a text message to a recipient. :param to: mobile number (without + or 00) or Jid of the recipient :type to: str :param msg: message text to transmit. :type msg: str|bytes :return: future for async notification """ if isinstance(msg, str): msg = msg.encode("utf-8") outgoingMessage = TextMessageProtocolEntity(msg, to=self.normalize_jid(to)) self.toLower(outgoingMessage) # Assume successful transmission fut = Future() fut.set_result("Done") return fut
def async_call(self, f: Callable[..., Any], *a: Any, **kw: Any) -> Any: ''' run a callback function on the main thread and return its value (blocking). the callback receives 'self' as an argument. ''' if not shutdown: msg = 'running {} on main thread blocking' self.log.debug2(lambda: msg.format(format_funcall(f.__name__, a, kw))) result_fut = futures.Future() # type: futures.Future @functools.wraps(f) def cb() -> None: result_fut.set_result(f(self, *a, **kw)) self._run_on_main_thread(cb) result = result_fut.result() self.log.debug2(lambda: f'async returns {result}') return result else: return f(self, *a, **kw)
def parent_callback(self, parent_fu): ''' Callback from executor future to update the parent. Args: - executor_fu (Future): Future returned by the executor along with callback Returns: - None Updates the super() with the result() or exception() ''' if parent_fu.done() is True: e = parent_fu._exception if e: super().set_exception(e) else: super().set_result(parent_fu.result()) return
def launch_task(self, task_id, executable, *args, **kwargs): ''' Handle the actual submission of the task to the executor layer We should most likely add a callback at this point Args: task_id (uuid string) : A uuid string that uniquely identifies the task executable (callable) : A callable object args (list of positional args) kwargs (list of keyword args) Returns: Future that tracks the execution of the submitted executable ''' #logger.debug("Submitting to executor : %s", task_id) exec_fu = self.executor.submit(executable, *args, **kwargs) exec_fu.add_done_callback(partial(self.handle_update, task_id)) return exec_fu
def parent_callback(self, executor_fu): ''' Callback from executor future to update the parent. Args: - executor_fu (Future): Future returned by the executor along with callback Returns: - None Updates the super() with the result() or exception() ''' if executor_fu.done() == True: try : super().set_result(executor_fu.result()) except Exception as e: super().set_exception(e)
def __init__ (self, parent, tid=None, stdout=None, stderr=None): ''' Initialize the AppFuture. Args: - parent (Future) : The parent future if one exists A default value of None should be passed in if app is not launched KWargs: - tid (Int) : Task id should be any unique identifier. Now Int. - stdout (str) : Stdout file of the app. Default: None - stderr (str) : Stderr file of the app. Default: None ''' self._tid = tid super().__init__() self.parent = parent #if self.parent: # parent.add_done_callback(self.parent_callback) self._outputs = [] self._stdout = stdout self._stderr = stderr
def ensure_future(coro_or_future, *, loop=None): """Wrap a coroutine or an awaitable in a future. If the argument is a Future, it is returned directly. """ if futures.isfuture(coro_or_future): if loop is not None and loop is not coro_or_future._loop: raise ValueError('loop argument must agree with Future') return coro_or_future elif coroutines.iscoroutine(coro_or_future): if loop is None: loop = events.get_event_loop() task = loop.create_task(coro_or_future) if task._source_traceback: del task._source_traceback[-1] return task elif compat.PY35 and inspect.isawaitable(coro_or_future): return ensure_future(_wrap_awaitable(coro_or_future), loop=loop) else: raise TypeError('A Future, a coroutine or an awaitable is required')
def run_coroutine_threadsafe(coro, loop): """Submit a coroutine object to a given event loop. Return a concurrent.futures.Future to access the result. """ if not coroutines.iscoroutine(coro): raise TypeError('A coroutine object is required') future = concurrent.futures.Future() def callback(): try: futures._chain_future(ensure_future(coro, loop=loop), future) except Exception as exc: if future.set_running_or_notify_cancel(): future.set_exception(exc) raise loop.call_soon_threadsafe(callback) return future
def close(self): from threading import Lock lock = Lock() count = [0] future = Future() def on_close(_): with lock: count[0] += 1 if count[0] == len(self.reporters): future.set_result(True) for reporter in self.reporters: f = reporter.close() f.add_done_callback(on_close) return future
def _wakeup(self, future): try: future.result() except Exception as exc: # This may also be a cancellation. self._step(exc) else: # Don't pass the value of `future.result()` explicitly, # as `Future.__iter__` and `Future.__await__` don't need it. # If we call `_step(value, None)` instead of `_step()`, # Python eval loop would use `.send(value)` method call, # instead of `__next__()`, which is slower for futures # that return non-generator iterators from their `__iter__`. self._step() self = None # Needed to break cycles when an exception occurs. # wait() and as_completed() similar to those in PEP 3148.
def ensure_future(coro_or_future, *, loop=None): """Wrap a coroutine or an awaitable in a future. If the argument is a Future, it is returned directly. """ if isinstance(coro_or_future, futures.Future): if loop is not None and loop is not coro_or_future._loop: raise ValueError('loop argument must agree with Future') return coro_or_future elif coroutines.iscoroutine(coro_or_future): if loop is None: loop = events.get_event_loop() task = loop.create_task(coro_or_future) if task._source_traceback: del task._source_traceback[-1] return task elif compat.PY35 and inspect.isawaitable(coro_or_future): return ensure_future(_wrap_awaitable(coro_or_future), loop=loop) else: raise TypeError('A Future, a coroutine or an awaitable is required')
def run_in_executor(self, executor, func, *args): if (coroutines.iscoroutine(func) or coroutines.iscoroutinefunction(func)): raise TypeError("coroutines cannot be used with run_in_executor()") self._check_closed() if isinstance(func, events.Handle): assert not args assert not isinstance(func, events.TimerHandle) if func._cancelled: f = futures.Future(loop=self) f.set_result(None) return f func, args = func._callback, func._args if executor is None: executor = self._default_executor if executor is None: executor = concurrent.futures.ThreadPoolExecutor(_MAX_WORKERS) self._default_executor = executor return futures.wrap_future(executor.submit(func, *args), loop=self)
def _create_connection_transport(self, sock, protocol_factory, ssl, server_hostname): protocol = protocol_factory() waiter = futures.Future(loop=self) if ssl: sslcontext = None if isinstance(ssl, bool) else ssl transport = self._make_ssl_transport( sock, protocol, sslcontext, waiter, server_side=False, server_hostname=server_hostname) else: transport = self._make_socket_transport(sock, protocol, waiter) try: yield from waiter except: transport.close() raise return transport, protocol
def test_restart(self): self._new_instance = self.mox.CreateMock(instance.Instance) self.factory.new_instance(0, expect_ready_request=True).AndReturn( self._new_instance) f = futures.Future() f.set_result(True) module._THREAD_POOL.submit(self.module._start_instance, self._wsgi_server, self._new_instance).AndReturn(f) self._instance.quit(force=True) port = object() self.mox.ReplayAll() self.module.restart() self.mox.VerifyAll() self.assertEqual(self.module._handle_request, self._wsgi_server._app.func) self.assertEqual({'inst': self._new_instance}, self._wsgi_server._app.keywords) self.assertFalse(self.module._suspended)
def schedule(self, f, *args, **kwargs): """ Try to acquire connection access lock. Then call protocol method. Return concurrent Future instance you can wait in the other thread. """ self.wait_open() # RabbitMQ operations are multiplexed between different AMQP # method callbacks. Final result of the protocol method call # will be set inside one of this callbacks. So other thread # will be able to wait unless this event happens in the # connection event loop. future = Future() with self.lock: self.process(get_ident(), (f, args, kwargs), future) return future
def _coro_runner(self): while True: # Wait for a hand-off await disable_cancellation(_future_wait(self._request)) self._coro = self._request.result() self._request = Future() # If no coroutine, we're shutting down if not self._coro: break # Run the the coroutine try: self._result_value = await self._coro self._result_exc = None except BaseException as e: self._result_value = None self._result_exc = e # Hand it back to the thread self._done_evt.set() await self._terminate_evt.set()
def run_in_executor(self, executor, callback, *args): if coroutines.iscoroutinefunction(callback): raise TypeError("Coroutines cannot be used with run_in_executor()") if isinstance(callback, events.Handle): assert not args assert not isinstance(callback, events.TimerHandle) if callback._cancelled: f = futures.Future(loop=self) f.set_result(None) return f callback, args = callback._callback, callback._args if executor is None: executor = self._default_executor if executor is None: executor = concurrent.futures.ThreadPoolExecutor(_MAX_WORKERS) self._default_executor = executor return futures.wrap_future(executor.submit(callback, *args), loop=self)
def funcWrapper(*args) -> bool: """can be passed to executor, workflow step is loaded from args[0], funcWrapper wait for futures and stores values in steps before a launcher is started""" assert (len(args) == 1) aSWorkflowStepState = args[0] assert (isinstance(aSWorkflowStepState, states.WorkflowStepState)) # wait for prior future results for key, input in aSWorkflowStepState.inputPortStates.items(): currentValue = input.getEvaluation() # future if isinstance(currentValue, Future): # Quick and dirty: blocks until port-states are written by launchTool; # Later: this wont work in multi-CPU / multi-node enviroments because port states might be not in sync! input.setValue(currentValue.result()) logging.getLogger('system').debug(currentValue.result()) else: input.setValue(currentValue) launcher.launchTool(aSWorkflowStepState) return True
def mock_session_pools(f): """ Helper decorator that allows tests to initialize :class:.`Session` objects without actually connecting to a Cassandra cluster. """ @wraps(f) def wrapper(*args, **kwargs): with patch.object(Session, "add_or_renew_pool") as mocked_add_or_renew_pool: future = Future() future.set_result(object()) mocked_add_or_renew_pool.return_value = future f(*args, **kwargs) return wrapper
def __del__(self): if self.formatted_tb: app_log.error('Future exception was never retrieved: %s', ''.join(self.formatted_tb).rstrip())
def result(self, timeout=None): """If the operation succeeded, return its result. If it failed, re-raise its exception. This method takes a ``timeout`` argument for compatibility with `concurrent.futures.Future` but it is an error to call it before the `Future` is done, so the ``timeout`` is never used. """ self._clear_tb_log() if self._result is not None: return self._result if self._exc_info is not None: raise_exc_info(self._exc_info) self._check_done() return self._result
def exception(self, timeout=None): """If the operation raised an exception, return the `Exception` object. Otherwise returns None. This method takes a ``timeout`` argument for compatibility with `concurrent.futures.Future` but it is an error to call it before the `Future` is done, so the ``timeout`` is never used. """ self._clear_tb_log() if self._exc_info is not None: return self._exc_info[1] else: self._check_done() return None
def add_done_callback(self, fn): """Attaches the given callback to the `Future`. It will be invoked with the `Future` as its argument when the Future has finished running and its result is available. In Tornado consider using `.IOLoop.add_future` instead of calling `add_done_callback` directly. """ if self._done: fn(self) else: self._callbacks.append(fn)
def set_result(self, result): """Sets the result of a ``Future``. It is undefined to call any of the ``set`` methods more than once on the same object. """ self._result = result self._set_done()
def __del__(self): if not self._log_traceback: # set_exception() was not called, or result() or exception() # has consumed the exception return tb = traceback.format_exception(*self._exc_info) app_log.error('Future %r exception was never retrieved: %s', self, ''.join(tb).rstrip())
def set_exception(self, exception): """Sets the exception of a ``Future.``""" self.set_exc_info( (exception.__class__, exception, getattr(exception, '__traceback__', None)))