我们从Python开源项目中,提取了以下50个代码示例,用于说明如何使用concurrent.futures.TimeoutError()。
def test_zero_timeout(self): future1 = self.executor.submit(time.sleep, 2) completed_futures = set() try: for future in futures.as_completed( [CANCELLED_AND_NOTIFIED_FUTURE, EXCEPTION_FUTURE, SUCCESSFUL_FUTURE, future1], timeout=0): completed_futures.add(future) except futures.TimeoutError: pass self.assertEqual(set([CANCELLED_AND_NOTIFIED_FUTURE, EXCEPTION_FUTURE, SUCCESSFUL_FUTURE]), completed_futures)
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 await_exception(self, timeout=None): # pragma: no-cover """ Return the exception raised by the call that the future represents. Args: timeout: The number of seconds to wait for the exception if the future isn't done. If None, then there is no limit on the wait time. Returns: The exception raised by the call that the future represents or None if the call completed without raising. Raises: CancelledError: If the future was cancelled. TimeoutError: If the future didn't finish executing before the given timeout. """ self._wait_for_done(timeout) return self.exception()
def _check_executor(self, dt): start = time() try: for future in as_completed(self._futures[:], 0): self._futures.remove(future) try: result = future.result() except: traceback.print_exc() # make an error tile? continue if result is None: continue callback, args = result callback(*args) # capped executor in time, in order to prevent too much slowiness. # seems to works quite great with big zoom-in/out if time() - start > self.cap_time: break except TimeoutError: pass
def _check_executor(self, dt): start = time() try: for future in as_completed(self._futures[:], 0): self._futures.remove(future) try: result = future.result() except Exception: traceback.print_exc() # make an error tile? continue if result is None: continue callback, args = result callback(*args) # capped executor in time, in order to prevent too much # slowiness. # seems to works quite great with big zoom-in/out if time() - start > self.cap_time: break except TimeoutError: pass
def start_app(): """ Start the ExperimentManager as application """ global executor, threads threads.append(executor.submit(api.start_listening)) config_t = configuration.init_sys() cancel = False while True: for t in threads: try: if not cancel: t.result(timeout=3) else: if t.running(): t.cancel() except TimeoutError: continue except KeyboardInterrupt: logger.info("received ctrl-c, shutting down...") cancel = True if get_config('database', 'drop_on_exit', False).lower() == 'true': drop_tables() configuration.stop.set() config_t.join()
def concurrent_find(func, params, **kw): timeout = kw.pop("concurrent_timeout", None) with async(func, list(params), **kw) as futures: future = None try: for future in futures.as_completed(timeout=timeout): if not future.exception() and future.result(): futures.kill() return future.result() else: if future: return future.result() except FutureTimeoutError as exc: if not timeout: # ?? raise futures.kill() _logger.warning("Concurrent future timed out (%s)", exc)
def run(self): try: (save_option, verdict, apply_for_all) = self.future.result(PACKET_TIMEOUT) except TimeoutError: # What to do on timeouts? # Should we even have timeouts? self.pkt.accept() else: if RuleVerdict(verdict) == RuleVerdict.DROP: drop_packet(self.pkt, self.conn) else: self.pkt.accept()
def test_map_timeout(self): results = [] try: for i in self.executor.map(time.sleep, [0, 0, 3], timeout=1.5): results.append(i) except futures.TimeoutError: pass else: self.fail('expected TimeoutError') self.assertEqual([None, None], results)
def test_result_with_timeout(self): self.assertRaises(futures.TimeoutError, PENDING_FUTURE.result, timeout=0) self.assertRaises(futures.TimeoutError, RUNNING_FUTURE.result, timeout=0) self.assertRaises(futures.CancelledError, CANCELLED_FUTURE.result, timeout=0) self.assertRaises(futures.CancelledError, CANCELLED_AND_NOTIFIED_FUTURE.result, timeout=0) self.assertRaises(IOError, EXCEPTION_FUTURE.result, timeout=0) self.assertEqual(SUCCESSFUL_FUTURE.result(timeout=0), 42)
def test_exception_with_timeout(self): self.assertRaises(futures.TimeoutError, PENDING_FUTURE.exception, timeout=0) self.assertRaises(futures.TimeoutError, RUNNING_FUTURE.exception, timeout=0) self.assertRaises(futures.CancelledError, CANCELLED_FUTURE.exception, timeout=0) self.assertRaises(futures.CancelledError, CANCELLED_AND_NOTIFIED_FUTURE.exception, timeout=0) self.assertTrue(isinstance(EXCEPTION_FUTURE.exception(timeout=0), IOError)) self.assertEqual(SUCCESSFUL_FUTURE.exception(timeout=0), None)
def await_result(self, timeout=None): """ Return the result of the call that the future represents. 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 since the eventloop will be blocked. ---> To be safe, don't use await_result() in a Qt slot. You should rather use result() and add_done_callback() instead. Args: timeout: The number of seconds to wait for the result if the future isn't done. If None, then there is no limit on the wait time. Returns: The result of the call that the future represents. Raises: CancelledError: If the future was cancelled. TimeoutError: If the future didn't finish executing before the given timeout. Exception: If the call raised then that exception will be raised. """ self._wait_for_done(timeout) return self.result()
def send_sync(self, msg, timeout=long_timeout) -> None: try: self.send_thread(msg, False).result(timeout) except TimeoutError as e: self.log.warn(f'timed out waiting for processing of {msg} in {self.name}') return self.data
def cancel_job(self, job_id): if self.job_future: while True: if self.is_spark_session_available(): self.spark_session.sparkContext.cancelAllJobs() try: self.job_future.result(timeout=1) break except TimeoutError as te: pass message = self.MNN007[1].format(self.app_id) log.info(message) self._generate_output(message, 'SUCCESS', self.MNN007[0])
def test_map_timeout(self): results = [] try: for i in self.executor.map(time.sleep, [0, 0, 6], timeout=5): results.append(i) except futures.TimeoutError: pass else: self.fail('expected TimeoutError') self.assertEqual([None, None], results)
def test_result_with_timeout(self): self.assertRaises(futures.TimeoutError, PENDING_FUTURE.result, timeout=0) self.assertRaises(futures.TimeoutError, RUNNING_FUTURE.result, timeout=0) self.assertRaises(futures.CancelledError, CANCELLED_FUTURE.result, timeout=0) self.assertRaises(futures.CancelledError, CANCELLED_AND_NOTIFIED_FUTURE.result, timeout=0) self.assertRaises(OSError, EXCEPTION_FUTURE.result, timeout=0) self.assertEqual(SUCCESSFUL_FUTURE.result(timeout=0), 42)
def test_exception_with_timeout(self): self.assertRaises(futures.TimeoutError, PENDING_FUTURE.exception, timeout=0) self.assertRaises(futures.TimeoutError, RUNNING_FUTURE.exception, timeout=0) self.assertRaises(futures.CancelledError, CANCELLED_FUTURE.exception, timeout=0) self.assertRaises(futures.CancelledError, CANCELLED_AND_NOTIFIED_FUTURE.exception, timeout=0) self.assertTrue(isinstance(EXCEPTION_FUTURE.exception(timeout=0), OSError)) self.assertEqual(SUCCESSFUL_FUTURE.exception(timeout=0), None)
def wait(fs, *, loop=None, timeout=None, return_when=ALL_COMPLETED): """Wait for the Futures and coroutines given by fs to complete. The sequence futures must not be empty. Coroutines will be wrapped in Tasks. Returns two sets of Future: (done, pending). Usage: done, pending = yield from asyncio.wait(fs) Note: This does not raise TimeoutError! Futures that aren't done when the timeout occurs are returned in the second set. """ if futures.isfuture(fs) or coroutines.iscoroutine(fs): raise TypeError("expect a list of futures, not %s" % type(fs).__name__) if not fs: raise ValueError('Set of coroutines/Futures is empty.') if return_when not in (FIRST_COMPLETED, FIRST_EXCEPTION, ALL_COMPLETED): raise ValueError('Invalid return_when value: {}'.format(return_when)) if loop is None: loop = events.get_event_loop() fs = {ensure_future(f, loop=loop) for f in set(fs)} return (yield from _wait(fs, timeout, return_when, loop))
def test_timeout_future(self): with self.assertRaises(futures.TimeoutError): future = self.async_future() # Do not call self.wait() future.result(timeout=.1)
def test_download_timeout(self, exists, isdir): exists.return_value = True isdir.return_value = True with self.assertRaises(TimeoutError): downloader = Downloader('test', bucket='serenata-de-amor-data', region_name='a-east-1', timeout=0.001) downloader.url = Mock(return_value="http://www.google.com:81/") loop = asyncio.get_event_loop() with ClientSession(loop=loop) as client: yield from downloader.fetch_file(client, '2016-12-06-reibursements.xz')
def time_limited(chalice_app: DSSChaliceApp): """ When this decorator is applied to a route handler, we will process the request in a secondary thread. If the processing exceeds the time allowed, we will return a standardized error message. """ def real_decorator(method: callable): @functools.wraps(method) def wrapper(*args, **kwargs): executor = ThreadPoolExecutor() try: future = executor.submit(method, *args, **kwargs) time_remaining_s = chalice_app._override_exptime_seconds # type: typing.Optional[float] if time_remaining_s is None: time_remaining_s = min( API_GATEWAY_TIMEOUT_SECONDS, chalice_app.lambda_context.get_remaining_time_in_millis() / 1000) time_remaining_s = max(0.0, time_remaining_s - EXECUTION_TERMINATION_THRESHOLD_SECONDS) try: chalice_response = future.result(timeout=time_remaining_s) return chalice_response except TimeoutError: return timeout_response() finally: executor.shutdown(wait=False) return wrapper return real_decorator
def test_on_timeout_exception(self): result = ApiGateway().on_exception(ex=TimeoutError()) assert_that(result['statusCode'], equal_to(504)) assert_that(result['body'], equal_to('Execution is about to timeout.'))
def on_exception(self, ex): logging.exception(str(ex)) if type(ex) == TimeoutError: return http_response("Execution is about to timeout.", status=504) else: return http_response('Internal Server Error', status=500)
def wait(fs, *, loop=None, timeout=None, return_when=ALL_COMPLETED): """Wait for the Futures and coroutines given by fs to complete. The sequence futures must not be empty. Coroutines will be wrapped in Tasks. Returns two sets of Future: (done, pending). Usage: done, pending = yield from asyncio.wait(fs) Note: This does not raise TimeoutError! Futures that aren't done when the timeout occurs are returned in the second set. """ if isinstance(fs, futures.Future) or coroutines.iscoroutine(fs): raise TypeError("expect a list of futures, not %s" % type(fs).__name__) if not fs: raise ValueError('Set of coroutines/Futures is empty.') if return_when not in (FIRST_COMPLETED, FIRST_EXCEPTION, ALL_COMPLETED): raise ValueError('Invalid return_when value: {}'.format(return_when)) if loop is None: loop = events.get_event_loop() fs = {ensure_future(f, loop=loop) for f in set(fs)} return (yield from _wait(fs, timeout, return_when, loop))
def __exit__(self, exc_type, exc_val, exc_tb): if exc_type is futures.CancelledError and self._cancelled: self._cancel_handler = None self._task = None raise futures.TimeoutError self._cancel_handler.cancel() self._cancel_handler = None self._task = None
def task_done(future): # Stores FPS for each stream f = open("FPS.txt", "a") try: result = future.result() # blocks until results are ready except TimeoutError as e: print("Function took longer than %d seconds" % e.args[1]) except Exception as e: print("Function raised %s" % e) print(e.traceback) # traceback of the function else: lock.acquire() f.write(str(result)+"\n") lock.release() f.close()
def task_done(future): # Stores FPS for each camera f = open("sources.txt", "a") try: result = future.result() # blocks until results are ready except TimeoutError as e: print("Function took longer than %d seconds" % e.args[1]) except Exception as e: print("Function raised %s" % e) print(e.traceback) # traceback of the function else: lock.acquire() f.write(str(result)+"\n") lock.release() f.close()
def _get_events(self): with ThreadPoolExecutor(max_workers=1) as executor: future = None while self.running: if future is None: future = executor.submit(self.stream.next) try: yield future.result(timeout=0.1) future = None except TimeoutError: self.producer.wake()
def run_loop(self) -> None: """ Listen to messages with data to train on, and return trained models with a report on model quality. If several messages with the same id arrive, result of only the last one will be sent back. This method loops until a message to stop is received (sent only from tests). """ jobs = OrderedDict() # type: Dict[str, Future] with ThreadPoolExecutor(max_workers=1) as pool: while True: to_submit = {} for message in self.consumer: value, should_stop = self.extract_value(message) if should_stop: return elif value is not None: to_submit[value['workspace_id']] = value for ws_id, value in to_submit.items(): if ws_id in jobs: _, future = jobs[ws_id] future.cancel() jobs[ws_id] = (value, pool.submit(self.train_model, value)) sent = [] for ws_id, (request, future) in jobs.items(): try: result = future.result(timeout=0) except TimeoutError: pass else: self.send_result(result, request) sent.append(ws_id) for ws_id in sent: del jobs[ws_id]
def individual_abandon_example() -> None: """Example of individual abandonment.""" submitted_futures = [EXECUTOR.submit(rest, n) for n in [1, 1, 2, 3, 5, 7, 13, 4, 6, 8, 9, 10, 11, 12]] try: for completed_future in futures.as_completed(submitted_futures, timeout=5): print("completed: {}".format(completed_future.result())) except futures.TimeoutError: print("timeout occurred - abandoning remaining futures.") for s_future in submitted_futures: if not s_future.done(): s_future.abandon()
def map_example() -> None: """Example of an executor map.""" try: list(EXECUTOR.map(rest, [1, 1, 2, 3, 5, 7, 13, 4, 6, 8, 9, 10, 11, 12], timeout=5)) except futures.TimeoutError: print("timed out")
def shutdown_wait_example() -> None: """Example of shutting down and waiting.""" try: list(EXECUTOR.map(rest, [1, 1, 2, 3, 5, 7, 13, 4, 6, 8, 9, 10, 11, 12], timeout=5)) except futures.TimeoutError: print("timed out") EXECUTOR.shutdown() print("shut down and stopped")