Python concurrent.futures.TimeoutError() Examples

The following are 30 code examples of concurrent.futures.TimeoutError(). 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 concurrent.futures , or try the search function .
Example #1
Source File: computation.py    From dwave-cloud-client with Apache License 2.0 6 votes vote down vote up
def wait_id(self, timeout=None):
        """Blocking id getter.

        Return the submitted problem ID, but unlike :meth:`.id`, block until the
        ID becomes known, or until `timeout` expires.

        Args:
            timeout (float, default=None):
                Timeout in seconds. By default, wait indefinitely for problem
                id to become known/available.

        Returns:
            str:
                Problem ID, as returned by SAPI.

        Raises:
            :exc:`concurrent.futures.TimeoutError`:
                When `timeout` exceeded, and problem id not ready.

        """
        if not self._id_ready_event.wait(timeout=timeout):
            raise TimeoutError("problem id not available yet")

        return self._id 
Example #2
Source File: downloader.py    From PyCon-Mobile-App with GNU General Public License v3.0 6 votes vote down vote up
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 
Example #3
Source File: partitioning.py    From gcp-variant-transforms with Apache License 2.0 6 votes vote down vote up
def _copy_to_flatten_table(self, output_table_id, cp_query):
    job_config = bigquery.job.QueryJobConfig(destination=output_table_id)
    query_job = self._client.query(cp_query, job_config=job_config)
    num_retries = 0
    while True:
      try:
        _ = query_job.result(timeout=600)
      except TimeoutError as e:
        logging.warning('Time out waiting for query: %s', cp_query)
        if num_retries < bigquery_util.BQ_NUM_RETRIES:
          num_retries += 1
          time.sleep(90)
        else:
          logging.error('Copy to table query failed: %s', output_table_id)
          raise e
      else:
        break
    logging.info('Copy to table query was successful: %s', output_table_id) 
Example #4
Source File: connection.py    From switchio with Mozilla Public License 2.0 6 votes vote down vote up
def connect_and_auth(host, port, password, prot, loop, log, timeout=0.5):
    """Try to create a connection and authenticate to the
    target FS ESL.
    """
    msg = ("Failed to connect to server at '{}:{}'\n"
           "Please check that FreeSWITCH is running and "
           "accepting ESL connections.".format(host, port))
    try:
        await asyncio.wait_for(
            loop.create_connection(lambda: prot, host, port),
            timeout=timeout)
    except (
        ConnectionRefusedError, asyncio.TimeoutError, OSError,
        futures.TimeoutError,
    ) as err:
        raise ConnectionError(msg.format(host, port))

    # TODO: consider using the asyncio_timeout lib here
    try:
        await asyncio.wait_for(prot.authenticate(), timeout)
    except asyncio.TimeoutError:
        raise ConnectionRefusedError(msg.format(host, port)) 
Example #5
Source File: TestRailPreRunModifier.py    From robotframework-testrail with Apache License 2.0 6 votes vote down vote up
def start_suite(self, suite: TestSuite) -> None:
        """Form list of tests for the Robot Framework test suite that are included in the TestRail test run.

        If analysis depth of the run results is greater than zero, when first suite is launched
        a list of 'testrailid' tags of stable test cases is obtained.
        After that the list of tags is written to the class attribute and for subsequent suites the obtaining is not happening.

        If analysis depth of the run results is zero, when the first suite is launched
        a list of 'testrailid' tags of all test cases in the given status is obtained.
        After that the list of tags is written to the class attribute and for subsequent suites the obtaining is not happening.

        *Args:*\n
            _suite_ - Robot Framework test suite object.
        """
        tests = suite.tests
        suite.tests = None
        try:
            if self.results_depth > 0:
                suite.tests = [t for t in tests if (set(t.tags) & set(self.tr_stable_tags_list))]
            else:
                suite.tests = [t for t in tests if (set(t.tags) & set(self.tr_tags_list))]
        except (RequestException, TimeoutError) as error:
            self._log_to_parent_suite(suite, str(error)) 
Example #6
Source File: segmented.py    From streamlink with BSD 2-Clause "Simplified" License 6 votes vote down vote up
def run(self):
        while not self.closed:
            try:
                segment, future = self.futures.get(block=True, timeout=0.5)
            except queue.Empty:
                continue

            # End of stream
            if future is None:
                break

            while not self.closed:
                try:
                    result = future.result(timeout=0.5)
                except futures.TimeoutError:
                    continue
                except futures.CancelledError:
                    break

                if result is not None:
                    self.write(segment, result)

                break

        self.close() 
Example #7
Source File: partitioning.py    From gcp-variant-transforms with Apache License 2.0 6 votes vote down vote up
def _run_query(self, query):
    query_job = self._client.query(query)
    num_retries = 0
    while True:
      try:
        iterator = query_job.result(timeout=300)
      except TimeoutError as e:
        logging.warning('Time out waiting for query: %s', query)
        if num_retries < bigquery_util.BQ_NUM_RETRIES:
          num_retries += 1
          time.sleep(90)
        else:
          raise e
      else:
        break
    result = []
    for i in iterator:
      result.append(str(i.values()[0]))
    return result 
Example #8
Source File: models.py    From switchio with Mozilla Public License 2.0 6 votes vote down vote up
def poll(self, events, timeout=None,
                   return_when=asyncio.FIRST_COMPLETED):
        """Poll for any of a set of event types to be received for this session.
        """
        awaitables = {}
        for name in events:
            awaitables[self.recv(name)] = name
        done, pending = await asyncio.wait(
            awaitables, timeout=timeout, return_when=return_when)

        if done:
            ev_dicts = []
            for fut in done:
                awaitables.pop(fut)
                ev_dicts.append(fut.result())
            return ev_dicts, awaitables.values()
        else:
            raise asyncio.TimeoutError(
                "None of {} was received in {} seconds"
                .format(events, timeout))

    # call control / 'mod_commands' methods
    # TODO: dynamically add @decorated functions to this class
    # and wrap them using functools.update_wrapper ...? 
Example #9
Source File: auth.py    From mautrix-hangouts with GNU Affero General Public License v3.0 6 votes vote down vote up
def _receive_credential(self, result_type: CredentialType) -> Optional[str]:
        if self.cancelled:
            return None
        self.current_status = {
            "next_step": result_type.value,
        }
        if result_type == CredentialType.AUTHORIZATION:
            self.current_status["manual_auth_url"] = make_login_url(self.device_name)
        try:
            return self.queue.send_to_async(self.current_status,
                                            lambda: self._set_expecting(result_type))
        except futures.TimeoutError:
            self.cancel()
            return None
        except futures.CancelledError:
            return None 
Example #10
Source File: test_concurrent_futures.py    From Fluid-Designer with GNU General Public License v3.0 6 votes vote down vote up
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) 
Example #11
Source File: robot.py    From honey with MIT License 6 votes vote down vote up
def run(self):
        self.brain.connect()
        self.rtm_connect()
        if not self.client.server.connected:
            raise RuntimeError(
                'Can not connect to slack client. Check your settings.'
            )

        while True:
            events = self.read_message()
            if events:
                messages = self.extract_messages(events)
                if messages:
                    with ThreadPoolExecutor(max_workers=MAX_WORKERS) as executor:
                        try:
                            executor.map(self.handle_message, messages)
                        except TimeoutError:
                            self.logger.error(traceback.format_exc())
            else:
                time.sleep(0.3) 
Example #12
Source File: subscriber.py    From python-pubsub with Apache License 2.0 6 votes vote down vote up
def receive_messages_with_custom_attributes(project_id, subscription_id, timeout=None):
    """Receives messages from a pull subscription."""
    # [START pubsub_subscriber_async_pull_custom_attributes]
    from concurrent.futures import TimeoutError
    from google.cloud import pubsub_v1

    # TODO(developer)
    # project_id = "your-project-id"
    # subscription_id = "your-subscription-id"
    # Number of seconds the subscriber should listen for messages
    # timeout = 5.0

    subscriber = pubsub_v1.SubscriberClient()
    subscription_path = subscriber.subscription_path(project_id, subscription_id)

    def callback(message):
        print("Received message: {}".format(message.data))
        if message.attributes:
            print("Attributes:")
            for key in message.attributes:
                value = message.attributes.get(key)
                print("{}: {}".format(key, value))
        message.ack()

    streaming_pull_future = subscriber.subscribe(subscription_path, callback=callback)
    print("Listening for messages on {}..\n".format(subscription_path))

    # Wrap subscriber in a 'with' block to automatically call close() when done.
    with subscriber:
        try:
            # When `timeout` is not set, result() will block indefinitely,
            # unless an exception is encountered first.
            streaming_pull_future.result(timeout=timeout)
        except TimeoutError:
            streaming_pull_future.cancel()
    # [END pubsub_subscriber_async_pull_custom_attributes] 
Example #13
Source File: downloader.py    From garden.mapview with MIT License 6 votes vote down vote up
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 
Example #14
Source File: funcs.py    From NotSoBot with MIT License 6 votes vote down vote up
def queue_message(self, channel_id:str, msg):
		embed = '0'
		if type(msg) == discord.Embed:
			embed = '1'
			msg = jsonpickle.encode(msg)
		else:
			msg = str(msg)
		message_id = random.randint(0, 1000000)
		payload = {'key': 'verysecretkey', 'id': message_id, 'channel_id': channel_id, 'message': msg, 'embed': embed}
		try:
			with aiohttp.Timeout(15):
				async with self.session.post('http://ip:port/queue', data=payload) as r:
					return True
		except (asyncio.TimeoutError, aiohttp.errors.ClientConnectionError, aiohttp.errors.ClientError):
			await asyncio.sleep(5)
			return
		except Exception as e:
			print('queue error: '+str(e)) 
Example #15
Source File: funcs.py    From NotSoBot with MIT License 6 votes vote down vote up
def run_process(self, code, response=False):
		try:
			loop = self.bot.loop
			exit_future = asyncio.Future(loop=loop)
			create = loop.subprocess_exec(lambda: DataProtocol(exit_future),
																		*code, stdin=None, stderr=None)
			transport, protocol = await asyncio.wait_for(create, timeout=30)
			await exit_future
			if response:
				data = bytes(protocol.output)
				return data.decode('ascii').rstrip()
			return True
		except asyncio.TimeoutError:
			return False
		except Exception as e:
			print(e)
		finally:
			transport.close() 
Example #16
Source File: test_concurrent_futures.py    From Project-New-Reign---Nemesis-Main with GNU General Public License v3.0 6 votes vote down vote up
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) 
Example #17
Source File: tests_subscriptions.py    From opcua-modeling-tool with MIT License 6 votes vote down vote up
def test_events_wrong_source(self):
        objects = self.srv.get_objects_node()
        o = objects.add_object(3, 'MyObject')
        evgen = self.srv.get_event_generator(source=o)

        myhandler = MySubHandler()
        sub = self.opc.create_subscription(100, myhandler)
        handle = sub.subscribe_events()

        tid = datetime.utcnow()
        msg = b"this is my msg "
        evgen.trigger(tid, msg)

        with self.assertRaises(TimeoutError):  # we should not receive event
            ev = myhandler.future.result(2)

        # time.sleep(0.1)
        sub.unsubscribe(handle)
        sub.delete() 
Example #18
Source File: test.py    From Tomorrow with MIT License 6 votes vote down vote up
def test_wait(self):

        mutable = []

        @threads(N)
        def side_effects():
            mutable.append(True)

        result = side_effects()
        result._wait()
        assert mutable[0]

        @threads(N, timeout=0.1)
        def side_effects_timeout():
            time.sleep(1) 

        result = side_effects_timeout()
        with self.assertRaises(TimeoutError):
            result._wait() 
Example #19
Source File: worker.py    From biggraphite with Apache License 2.0 6 votes vote down vote up
def submit(self, label, command, opts):
        """Submit a bgutil command to run it asynchronously."""
        task = BgUtilTask(label, datetime.now())
        self.tasks.append(task)

        def _done_callback(f):
            try:
                result = f.result()
                task.completed(result)
            except futures.CancelledError as e:
                task.cancelled(e)
            except futures.TimeoutError as e:
                task.timed_out(e)
            except Exception as e:
                task.failed(e)

        future = self._executor.submit(
            self._wrap_command, task, context.accessor, command, opts
        )
        task.submitted()
        future.add_done_callback(_done_callback) 
Example #20
Source File: subscriber.py    From python-pubsub with Apache License 2.0 6 votes vote down vote up
def receive_messages(project_id, subscription_id, timeout=None):
    """Receives messages from a pull subscription."""
    # [START pubsub_subscriber_async_pull]
    # [START pubsub_quickstart_subscriber]
    from concurrent.futures import TimeoutError
    from google.cloud import pubsub_v1

    # TODO(developer)
    # project_id = "your-project-id"
    # subscription_id = "your-subscription-id"
    # Number of seconds the subscriber should listen for messages
    # timeout = 5.0

    subscriber = pubsub_v1.SubscriberClient()
    # The `subscription_path` method creates a fully qualified identifier
    # in the form `projects/{project_id}/subscriptions/{subscription_id}`
    subscription_path = subscriber.subscription_path(project_id, subscription_id)

    def callback(message):
        print("Received message: {}".format(message))
        message.ack()

    streaming_pull_future = subscriber.subscribe(subscription_path, callback=callback)
    print("Listening for messages on {}..\n".format(subscription_path))

    # Wrap subscriber in a 'with' block to automatically call close() when done.
    with subscriber:
        try:
            # When `timeout` is not set, result() will block indefinitely,
            # unless an exception is encountered first.
            streaming_pull_future.result(timeout=timeout)
        except TimeoutError:
            streaming_pull_future.cancel()
    # [END pubsub_subscriber_async_pull]
    # [END pubsub_quickstart_subscriber] 
Example #21
Source File: test_mock_submission.py    From dwave-cloud-client with Apache License 2.0 6 votes vote down vote up
def test_id_getter_setter(self):
        """Future.get_id/get_id works in isolation as expected."""

        f = Future(solver=None, id_=None)

        # f.id should be None
        self.assertIsNone(f.id)
        with self.assertRaises(TimeoutError):
            f.wait_id(timeout=1)

        # set it
        submission_id = 'test-id'
        f.id = submission_id

        # validate it's available
        self.assertEqual(f.wait_id(), submission_id)
        self.assertEqual(f.wait_id(timeout=1), submission_id)
        self.assertEqual(f.id, submission_id) 
Example #22
Source File: test_concurrent_futures.py    From ironpython3 with Apache License 2.0 6 votes vote down vote up
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) 
Example #23
Source File: _base.py    From loky with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def map(self, fn, *iterables, **kwargs):
            """Returns an iterator equivalent to map(fn, iter).

            Args:
                fn: A callable that will take as many arguments as there are
                    passed iterables.
                timeout: The maximum number of seconds to wait. If None, then
                    there is no limit on the wait time.
                chunksize: The size of the chunks the iterable will be broken
                    into before being passed to a child process. This argument
                    is only used by ProcessPoolExecutor; it is ignored by
                    ThreadPoolExecutor.

            Returns:
                An iterator equivalent to: map(func, *iterables) but the calls
                may be evaluated out-of-order.

            Raises:
                TimeoutError: If the entire result iterator could not be
                    generated before the given timeout.
                Exception: If fn(*args) raises for any values.
            """
            timeout = kwargs.get('timeout')
            if timeout is not None:
                end_time = timeout + time.time()

            fs = [self.submit(fn, *args) for args in zip(*iterables)]

            # Yield must be hidden in closure so that the futures are submitted
            # before the first iterator value is required.
            def result_iterator():
                try:
                    for future in fs:
                        if timeout is None:
                            yield future.result()
                        else:
                            yield future.result(end_time - time.time())
                finally:
                    for future in fs:
                        future.cancel()
            return result_iterator() 
Example #24
Source File: _test_process_executor.py    From loky with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def test_zero_timeout(self):
        future1 = self.executor.submit(time.sleep, 2)
        completed_futures = set()
        with pytest.raises(futures.TimeoutError):
            for future in futures.as_completed(
                    [CANCELLED_AND_NOTIFIED_FUTURE,
                     EXCEPTION_FUTURE,
                     SUCCESSFUL_FUTURE,
                     future1],
                    timeout=0):
                completed_futures.add(future)

        assert set([CANCELLED_AND_NOTIFIED_FUTURE, EXCEPTION_FUTURE,
                    SUCCESSFUL_FUTURE]) == completed_futures 
Example #25
Source File: tasks.py    From annotated-py-projects with MIT License 5 votes vote down vote up
def as_completed(fs, *, loop=None, timeout=None):
    """Return an iterator whose values are coroutines.

    When waiting for the yielded coroutines you'll get the results (or
    exceptions!) of the original Futures (or coroutines), in the order
    in which and as soon as they complete.

    This differs from PEP 3148; the proper way to use this is:

        for f in as_completed(fs):
            result = yield from f  # The 'yield from' may raise.
            # Use result.

    If a timeout is specified, the 'yield from' will raise
    TimeoutError when the timeout occurs before all Futures are done.

    Note: The futures 'f' are not necessarily members of fs.
    """
    if isinstance(fs, futures.Future) or coroutines.iscoroutine(fs):
        raise TypeError("expect a list of futures, not %s" % type(fs).__name__)
    loop = loop if loop is not None else events.get_event_loop()

    #
    # futures.Future()对象集合
    #
    todo = {async(f, loop=loop) for f in set(fs)} 
Example #26
Source File: tasks.py    From annotated-py-projects with MIT License 5 votes vote down vote up
def _wait_for_one():
        #
        # 异步返回
        #
        f = yield from done.get()
        if f is None:
            # Dummy value from _on_timeout().
            raise futures.TimeoutError
        return f.result()  # May raise f.exception(). 
Example #27
Source File: _base.py    From loky with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def result(self, timeout=None):
            """Return the result of the call that the future represents.

            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.
            """
            with self._condition:
                if self._state in [CANCELLED, CANCELLED_AND_NOTIFIED]:
                    raise CancelledError()
                elif self._state == FINISHED:
                    return self.__get_result()

                self._condition.wait(timeout)

                if self._state in [CANCELLED, CANCELLED_AND_NOTIFIED]:
                    raise CancelledError()
                elif self._state == FINISHED:
                    return self.__get_result()
                else:
                    raise TimeoutError() 
Example #28
Source File: drone.py    From olympe with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def stop_video_streaming(self):
        """
        Stops the live video stream from the drone front camera

        :rtype: ReturnTuple
        """
        if self._pdraw is None:
            msg = "Cannot start streaming while the drone is not connected"
            self.logger.error(msg)
            return makeReturnTuple(ErrorCodeDrone.ERROR_BAD_STATE, msg)

        if self._pdraw.is_legacy():
            f = self._thread_loop.run_async(
                self._disable_legacy_video_streaming_impl)
            try:
                if not f.result_or_cancel(timeout=5):
                    msg = "Unable to disable legacy video streaming"
                    self.logger.error(msg)
                    return makeReturnTuple(ErrorCodeDrone.ERROR_BAD_STATE, msg)
            except FutureTimeoutError:
                msg = "Unable to disable legacy video streaming (timeout)"
                self.logger.error(msg)
                return makeReturnTuple(ErrorCodeDrone.ERROR_BAD_STATE, msg)

        try:
            if not self._pdraw.pause().result_or_cancel(timeout=5):
                msg = "Failed to pause video stream"
                self.logger.error(msg)
                return makeReturnTuple(ErrorCodeDrone.ERROR_BAD_STATE, msg)
            if not self._pdraw.close().result_or_cancel(timeout=5):
                msg = "Failed to close video stream"
                self.logger.error(msg)
                return makeReturnTuple(ErrorCodeDrone.ERROR_BAD_STATE, msg)
        except FutureTimeoutError:
            msg = "Failed to stop video stream (timeout)"
            self.logger.error(msg)
            return makeReturnTuple(ErrorCodeDrone.ERROR_BAD_STATE, msg)

        return makeReturnTuple(self.error_code_drones.OK, "Video stream paused") 
Example #29
Source File: funcs.py    From NotSoBot with MIT License 5 votes vote down vote up
def download(self, url:str, path:str):
		try:
			with aiohttp.Timeout(5):
				async with self.session.get(url) as resp:
					data = await resp.read()
					with open(path, "wb") as f:
						f.write(data)
		except asyncio.TimeoutError:
			return False 
Example #30
Source File: funcs.py    From NotSoBot with MIT License 5 votes vote down vote up
def bytes_download(self, url:str):
		try:
			with aiohttp.Timeout(5):
				async with self.session.get(url) as resp:
					data = await resp.read()
					b = BytesIO(data)
					b.seek(0)
					return b
		except asyncio.TimeoutError:
			return False
		except Exception as e:
			print(e)
			return False