當前位置: 首頁>>代碼示例>>Python>>正文


Python _base.TimeoutError方法代碼示例

本文整理匯總了Python中concurrent.futures._base.TimeoutError方法的典型用法代碼示例。如果您正苦於以下問題:Python _base.TimeoutError方法的具體用法?Python _base.TimeoutError怎麽用?Python _base.TimeoutError使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在concurrent.futures._base的用法示例。


在下文中一共展示了_base.TimeoutError方法的7個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。

示例1: loader

# 需要導入模塊: from concurrent.futures import _base [as 別名]
# 或者: from concurrent.futures._base import TimeoutError [as 別名]
def loader(coin, exchange, logger):
    """
    Retrieve data from exchange and return best bid and ask prices on specific coin.
    
    Args:
        :param coin: <string> coin name (on exchange)
        :param exchange: <string> exchange name
        :param logger: <logging.Logger> crawler's logger
        
    Returns:
        :return: <dict> with structure like: {'ask': best ask price, 'bid': best bid price}
            ({'ask': None, 'bid': None} if an exception was raised while getting data from url)
        
    """
    result = {'ask': None, 'bid': None}
    try:
        url = URL_MAP[exchange](coin)
        async with aiohttp.ClientSession() as session:
            response = await session.get(url)
            data = json.loads(await response.text())
            result = PARSE_MAP[exchange](data, coin)
    except TimeoutError as time_out_e:
        logger.warning('Parse/GET exception in {} exchange for {} coin: {}'
                       '(concurrent.futures._base.TimeoutError)'.format(exchange, coin, str(time_out_e)))
    except JSONDecodeError as json_e:
        logger.warning('Parse/GET exception in {} exchange for {} coin: {}\n'
                       'response: {}'.format(exchange, coin, str(json_e), await response.status))
    except Exception as e:
        logger.warning('Parse/GET exception in {} exchange for {} coin: {}\n'
                       '{}'.format(exchange, coin, str(e), format_exc()))
    finally:
        return result 
開發者ID:Cindicator,項目名稱:CindicatorArbitrageBot,代碼行數:34,代碼來源:data_loader.py

示例2: exchange_loader

# 需要導入模塊: from concurrent.futures import _base [as 別名]
# 或者: from concurrent.futures._base import TimeoutError [as 別名]
def exchange_loader(coins, exchange, logger):
    """
    Retrieve data from exchange and return best bid and ask prices on exchange coins.
    
    Args:
        :param coins: <list> of <string> coin name (in db)
        :param exchange: <string> exchange name
        :param logger: <logging.Logger> crawler's logger
        
    Returns:
        :return: <dict> where keys is coins names and value is <dict> with structure like:
            {'ask': best ask price, 'bid': best bid price} ({'ask': None, 'bid': None} if
            an exception was raised while getting data from url)
        
    """
    result = {coin: {'ask': None, 'bid': None} for coin in coins}
    try:
        url = URL_MAP[exchange](None)
        async with aiohttp.ClientSession() as session:
                response = await session.get(url)
                data = json.loads(await response.text())
                for coin in coins:
                    try:
                        result[coin] = PARSE_MAP[exchange](data, coin)
                    except Exception as e:
                        logger.warning('Parse data exception in {} exchange for {} coin: {}\n'
                                       '{}'.format(exchange, coin, str(e), format_exc()))
    except TimeoutError as time_out_e:
        logger.warning('Parse/GET exception in {} exchange: {}'
                       '(concurrent.futures._base.TimeoutError)'.format(exchange, str(time_out_e)))
    except JSONDecodeError as json_e:
        logger.warning('Parse/GET exception in {} exchange: {}\n'
                       'response: {}'.format(exchange, str(json_e), await response.status))
    except Exception as e:
        logger.warning('Parse/GET exception in {} exchange: {}\n'
                       '{}'.format(exchange, str(e), format_exc()))
    finally:
        return result 
開發者ID:Cindicator,項目名稱:CindicatorArbitrageBot,代碼行數:40,代碼來源:data_loader.py

示例3: resultWithWakeup

# 需要導入模塊: from concurrent.futures import _base [as 別名]
# 或者: from concurrent.futures._base import TimeoutError [as 別名]
def resultWithWakeup(self, statusUpdateFunction=None):
        """Poll the future, but wake up frequently (to allow for keyboard interrupts)."""
        hasSubscribed = [False]
        timeOfLastSubscription = [time.time()]
        isComplete = [False]

        try:
            while True:
                try:
                    if (statusUpdateFunction is not None and 
                            self._computedValue is not None and 
                            hasSubscribed[0] == False and 
                            time.time() > timeOfLastSubscription[0] + 1.0):
                        hasSubscribed[0] = True
                        def onSuccess(result):
                            hasSubscribed[0] = False
                            timeOfLastSubscription[0] = time.time()
                            if not isComplete[0]:
                                try:
                                    statusUpdateFunction(result)
                                except:
                                    logging.error("statusUpdateFunction threw an unexpected exception:\n%s", traceback.format_exc())

                        def onFailure(result):
                            logging.error("subscribing to computation statistics produced unexpected error: %s", result)
                            hasSubscribed[0] = False
                            timeOfLastSubscription[0] = time.time()

                        self._computedValue.get_stats({'onSuccess': onSuccess, 'onFailure': onFailure})
                        timeOfLastSubscription[0] = time.time()

                    return self.result(timeout=KEYBOARD_INTERRUPT_WAKEUP_INTERVAL)
                except Futures.TimeoutError:
                    pass
        finally:
            isComplete[0] = True
            if statusUpdateFunction is not None:
                statusUpdateFunction(None) 
開發者ID:ufora,項目名稱:ufora,代碼行數:40,代碼來源:Future.py

示例4: call

# 需要導入模塊: from concurrent.futures import _base [as 別名]
# 或者: from concurrent.futures._base import TimeoutError [as 別名]
def call(self, method, *params):
        msg_id = self.new_id()
        msg = {"id": msg_id,
               "method": method,
               "params": params}

        data = "%s\n" % json.dumps(msg)
        print('< %s' % data[:200] + (data[200:] and "...\n"), end='')
        self.writer.write(data.encode())

        try:
            #r = asyncio.ensure_future(self.notifier.wait_for(msg_id))
            r = asyncio.async(self.notifier.wait_for(msg_id))
            yield from asyncio.wait([r, self.notifier.task], timeout=30, return_when=asyncio.FIRST_COMPLETED)

            if self.notifier.task.done():
                raise self.notifier.task.exception()

            data = r.result()
            log = '> %s' % data
            print(log[:100] + (log[100:] and '...'))

        except TimeoutError:
            raise Exception("Request to server timed out.")

        return data 
開發者ID:slush0,項目名稱:epycyzm,代碼行數:28,代碼來源:epycyzm.py

示例5: _send_request_with_backoff

# 需要導入模塊: from concurrent.futures import _base [as 別名]
# 或者: from concurrent.futures._base import TimeoutError [as 別名]
def _send_request_with_backoff(self, req, sess):
        # workaround to support setting instance specific timeouts and maxretries. (Mainly because you can't pass `self` to a decorator)
        # For safety, retrying should only be performed on idempotent HTTP methods.
        # That's why I didn't include the APIError exception in the list of exceptions.
        return await backoff.on_exception(
            wait_gen=lambda: backoff.expo(factor=self.backoff_factor),
            exception=(
                _TooManyRequests,
                TimeoutError,
                ClientConnectionError,
            ),  # Aiohttp exception hierarchy: https://docs.aiohttp.org/en/stable/client_reference.html?highlight=exceptions#hierarchy-of-exceptions
            max_tries=self.max_retries,
            max_time=self.timeout,
        )(self._handle_send_requests)(sess, req) 
開發者ID:omarryhan,項目名稱:pyfy,代碼行數:16,代碼來源:async_client.py

示例6: _async_render

# 需要導入模塊: from concurrent.futures import _base [as 別名]
# 或者: from concurrent.futures._base import TimeoutError [as 別名]
def _async_render(self, *, url: str, script: str = None, scrolldown, sleep: int, wait: float, reload, content: Optional[str], timeout: Union[float, int], keep_page: bool, cookies: list = [{}]):
        """ Handle page creation and js rendering. Internal use for render/arender methods. """
        try:
            page = await self.browser.newPage()

            # Wait before rendering the page, to prevent timeouts.
            await asyncio.sleep(wait)

            if cookies:
                for cookie in cookies:
                    if cookie:
                        await page.setCookie(cookie)

            # Load the given page (GET request, obviously.)
            if reload:
                await page.goto(url, options={'timeout': int(timeout * 1000)})
            else:
                await page.goto(f'data:text/html,{self.html}', options={'timeout': int(timeout * 1000)})

            result = None
            if script:
                result = await page.evaluate(script)

            if scrolldown:
                for _ in range(scrolldown):
                    await page._keyboard.down('PageDown')
                    await asyncio.sleep(sleep)
            else:
                await asyncio.sleep(sleep)

            if scrolldown:
                await page._keyboard.up('PageDown')

            # Return the content of the page, JavaScript evaluated.
            content = await page.content()
            if not keep_page:
                await page.close()
                page = None
            return content, result, page
        except TimeoutError:
            await page.close()
            page = None
            return None 
開發者ID:psf,項目名稱:requests-html,代碼行數:45,代碼來源:requests_html.py

示例7: _handle_send_requests

# 需要導入模塊: from concurrent.futures import _base [as 別名]
# 或者: from concurrent.futures._base import TimeoutError [as 別名]
def _handle_send_requests(self, sess, r):
        logger.debug(r.url)
        res = await sess.request(
            url=r.get("url"),
            headers=r.get("headers"),
            data=r.get("data"),
            json=r.get("json"),
            method=r.get("method"),
            proxy=self.proxies,
            proxy_auth=self.proxy_auth,
            timeout=self._timeout_manager,
        )
        async with res:
            res.status_code = res.status
            if res.status_code == 204:
                res.json = {}
            else:
                res.json = await res.json(content_type=None) or {}
        try:
            res.raise_for_status()
        except TimeoutError as e:
            logger.error("\nRequest timed out, try increasing the timeout period\n")
            raise e
        except ClientResponseError as e:
            if res.status_code == 401:  # Automatically refresh and resend request
                if (
                    res.json.get("error", None).get("message", None)
                    == TOKEN_EXPIRED_MSG
                ):
                    old_auth_header = r["headers"]["Authorization"]
                    await self._refresh_token()  # Should either raise an error or refresh the token
                    new_auth_header = self._access_authorization_header
                    if new_auth_header == old_auth_header:
                        msg = "refresh_token() was successfully called but token wasn't refreshed. Execution stopped to avoid infinite looping."
                        logger.critical(msg)
                        raise RuntimeError(msg)
                    r["headers"].update(new_auth_header)
                    return await self._send_requests(r)
                else:
                    msg = (
                        res.json.get("error_description") or res.json
                    )  # If none, raise the whole JSON
                    raise AuthError(msg=msg, http_response=res, http_request=r, e=e)
            elif res.status_code == 429:  # Too many requests
                msg = _safe_getitem(res.json, "error", "message") or _safe_getitem(
                    res.json, "error_description"
                )
                raise _TooManyRequests(msg=msg, http_response=res, http_request=r, e=e)
            else:
                msg = _safe_getitem(res.json, "error", "message") or _safe_getitem(
                    res.json, "error_description"
                )
                raise ApiError(msg=msg, http_response=res, http_request=r, e=e)
        else:
            return res 
開發者ID:omarryhan,項目名稱:pyfy,代碼行數:57,代碼來源:async_client.py


注:本文中的concurrent.futures._base.TimeoutError方法示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。