本文整理汇总了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
示例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
示例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)
示例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
示例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)
示例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
示例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