本文整理汇总了Python中backoff.expo方法的典型用法代码示例。如果您正苦于以下问题:Python backoff.expo方法的具体用法?Python backoff.expo怎么用?Python backoff.expo使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类backoff
的用法示例。
在下文中一共展示了backoff.expo方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: publish
# 需要导入模块: import backoff [as 别名]
# 或者: from backoff import expo [as 别名]
def publish(client, body, topic):
"""Publish a message to a Pub/Sub topic."""
project = 'projects/{}'.format(utils.get_project_id())
dest_topic = project + '/topics/' + topic
@backoff.on_exception(
backoff.expo, HttpError, max_tries=3, giveup=utils.fatal_code)
def _do_request():
client.projects().topics().publish(
topic=dest_topic, body=body).execute()
try:
_do_request()
except HttpError as e:
logging.error(e)
raise PubSubException(e)
示例2: pull
# 需要导入模块: import backoff [as 别名]
# 或者: from backoff import expo [as 别名]
def pull(client, sub, endpoint):
"""Register a listener endpoint."""
subscription = get_full_subscription_name(utils.get_project_id(), sub)
body = {'pushConfig': {'pushEndpoint': endpoint}}
@backoff.on_exception(
backoff.expo, HttpError, max_tries=3, giveup=utils.fatal_code)
def _do_request():
client.projects().subscriptions().modifyPushConfig(
subscription=subscription, body=body).execute()
try:
_do_request()
except HttpError as e:
logging.error(e)
return 'Error', 500
return 'ok, 204'
示例3: glossary
# 需要导入模块: import backoff [as 别名]
# 或者: from backoff import expo [as 别名]
def glossary():
"""Get the ID of a glossary available to session (do not mutate/delete)."""
glossary_id = "test-{}".format(uuid.uuid4())
translate_v3_create_glossary.create_glossary(
PROJECT_ID, GLOSSARY_INPUT_URI, glossary_id
)
yield glossary_id
# cleanup
@backoff.on_exception(
backoff.expo, (DeadlineExceeded, GoogleAPICallError), max_time=60
)
def delete_glossary():
try:
translate_v3_delete_glossary.delete_glossary(
PROJECT_ID, glossary_id)
except NotFound as e:
# Ignoring this case.
print("Got NotFound, detail: {}".format(str(e)))
delete_glossary()
开发者ID:GoogleCloudPlatform,项目名称:python-docs-samples,代码行数:23,代码来源:translate_v3_batch_translate_text_with_glossary_test.py
示例4: glossary
# 需要导入模块: import backoff [as 别名]
# 或者: from backoff import expo [as 别名]
def glossary():
"""Get the ID of a glossary available to session (do not mutate/delete)."""
glossary_id = "must-start-with-letters-" + str(uuid.uuid1())
translate_v3_create_glossary.create_glossary(
PROJECT_ID, GLOSSARY_INPUT_URI, glossary_id
)
yield glossary_id
# clean up
@backoff.on_exception(
backoff.expo, (DeadlineExceeded, GoogleAPICallError), max_time=60
)
def delete_glossary():
try:
translate_v3_delete_glossary.delete_glossary(
PROJECT_ID, glossary_id)
except NotFound as e:
# Ignoring this case.
print("Got NotFound, detail: {}".format(str(e)))
delete_glossary()
示例5: glossary
# 需要导入模块: import backoff [as 别名]
# 或者: from backoff import expo [as 别名]
def glossary():
"""Get the ID of a glossary available to session (do not mutate/delete)."""
glossary_id = "must-start-with-letters-" + str(uuid.uuid1())
translate_v3_create_glossary.create_glossary(
PROJECT_ID, GLOSSARY_INPUT_URI, glossary_id
)
yield glossary_id
# cleanup
@backoff.on_exception(
backoff.expo, (DeadlineExceeded, GoogleAPICallError), max_time=60
)
def delete_glossary():
try:
translate_v3_delete_glossary.delete_glossary(
PROJECT_ID, glossary_id)
except NotFound as e:
# Ignoring this case.
print("Got NotFound, detail: {}".format(str(e)))
delete_glossary()
示例6: test_create_glossary
# 需要导入模块: import backoff [as 别名]
# 或者: from backoff import expo [as 别名]
def test_create_glossary(capsys):
try:
glossary_id = "test-{}".format(uuid.uuid4())
translate_v3_create_glossary.create_glossary(
PROJECT_ID, GLOSSARY_INPUT_URI, glossary_id
)
out, _ = capsys.readouterr()
# assert
assert "Created:" in out
assert "gs://cloud-samples-data/translation/glossary_ja.csv" in out
finally:
# cleanup
@backoff.on_exception(
backoff.expo, (DeadlineExceeded, GoogleAPICallError), max_time=60
)
def delete_glossary():
try:
translate_v3_delete_glossary.delete_glossary(
PROJECT_ID, glossary_id)
except NotFound as e:
# Ignoring this case.
print("Got NotFound, detail: {}".format(str(e)))
delete_glossary()
开发者ID:GoogleCloudPlatform,项目名称:python-docs-samples,代码行数:25,代码来源:translate_v3_create_glossary_test.py
示例7: test_label_video
# 需要导入模块: import backoff [as 别名]
# 或者: from backoff import expo [as 别名]
def test_label_video(
capsys, annotation_spec_set, instruction, dataset, cleaner):
@backoff.on_exception(
backoff.expo, ServerError, max_time=testing_lib.RETRY_DEADLINE)
def run_sample():
# Start labeling.
return label_video.label_video(
dataset.name, instruction.name, annotation_spec_set.name)
response = run_sample()
cleaner.append(response.operation.name)
out, _ = capsys.readouterr()
assert 'Label_video operation name: ' in out
# Cancels the labeling operation.
response.cancel()
assert response.cancelled() is True
示例8: test_label_text
# 需要导入模块: import backoff [as 别名]
# 或者: from backoff import expo [as 别名]
def test_label_text(capsys, annotation_spec_set, instruction, dataset, cleaner):
@backoff.on_exception(
backoff.expo, ServerError, max_time=testing_lib.RETRY_DEADLINE)
def run_sample():
# Start labeling.
return label_text.label_text(
dataset.name, instruction.name, annotation_spec_set.name)
response = run_sample()
cleaner.append(response.operation.name)
out, _ = capsys.readouterr()
assert 'Label_text operation name: ' in out
# Cancels the labeling operation.
response.cancel()
assert response.cancelled() is True
示例9: test_search_all_resources
# 需要导入模块: import backoff [as 别名]
# 或者: from backoff import expo [as 别名]
def test_search_all_resources(asset_dataset, capsys):
scope = "projects/{}".format(PROJECT)
query = "name:{}".format(DATASET)
# Dataset creation takes some time to propagate, so the dataset is not
# immediately searchable. Need some time before the snippet will pass.
@backoff.on_exception(
backoff.expo, (AssertionError), max_time=120
)
def eventually_consistent_test():
quickstart_searchallresources.search_all_resources(scope, query=query)
out, _ = capsys.readouterr()
assert DATASET in out
eventually_consistent_test()
开发者ID:GoogleCloudPlatform,项目名称:python-docs-samples,代码行数:18,代码来源:quickstart_searchallresources_test.py
示例10: create_subscriptions
# 需要导入模块: import backoff [as 别名]
# 或者: from backoff import expo [as 别名]
def create_subscriptions(client, sub, topic):
"""
Create a subscription in pub/sub.
:param client:
:param sub:
:param topic:
:return:
"""
project = 'projects/{}'.format(utils.get_project_id())
dest_sub = project + '/subscriptions/' + sub
dest_topic = project + '/topics/' + topic
body = {'topic': dest_topic}
logging.info("Create topic %sub on topic %s in project %s",sub, topic, project)
def _do_get_request():
return client.projects().subscriptions().get(
subscription=dest_sub).execute()
@backoff.on_exception(
backoff.expo, HttpError, max_tries=3, giveup=utils.fatal_code)
def _do_create_request():
res = client.projects().subscriptions().create(
name=dest_sub, body=body).execute()
logging.debug(res)
try:
_do_get_request()
except Exception as e:
if e.resp.status == 404:
logging.error(e)
_do_create_request()
else:
logging.error(e)
raise PubSubException(e)
示例11: create_topic
# 需要导入模块: import backoff [as 别名]
# 或者: from backoff import expo [as 别名]
def create_topic(client, topic):
"""
Check if topix exists if not create it.
:param client:
:param topic:
:return:
"""
project = 'projects/{}'.format(utils.get_project_id())
dest_topic = project + '/topics/' + topic
@backoff.on_exception(
backoff.expo, HttpError, max_tries=3, giveup=utils.fatal_code)
def _do_get_request():
return client.projects().topics().get(topic=dest_topic).execute()
@backoff.on_exception(
backoff.expo, HttpError, max_tries=3, giveup=utils.fatal_code)
def _do_create_request():
client.projects().topics().create(name=dest_topic, body={}).execute()
try:
_do_get_request()
except HttpError as e:
if e.resp.status == 404:
_do_create_request()
else:
logging.error(e)
raise PubSubException(e)
示例12: sync
# 需要导入模块: import backoff [as 别名]
# 或者: from backoff import expo [as 别名]
def sync(self):
@retry_pattern(backoff.expo, FacebookRequestError, max_tries=5, factor=5)
def do_request():
return self.account.get_ad_creatives(params={'limit': RESULT_RETURN_LIMIT})
ad_creative = do_request()
# Create the initial batch
api_batch = API.new_batch()
batch_count = 0
# This loop syncs minimal AdCreative objects
for a in ad_creative:
# Excecute and create a new batch for every 50 added
if batch_count % 50 == 0:
api_batch.execute()
api_batch = API.new_batch()
# Add a call to the batch with the full object
a.api_get(fields=self.fields(),
batch=api_batch,
success=partial(ad_creative_success, stream=self),
failure=ad_creative_failure)
batch_count += 1
# Ensure the final batch is executed
api_batch.execute()
示例13: __iter__
# 需要导入模块: import backoff [as 别名]
# 或者: from backoff import expo [as 别名]
def __iter__(self):
@retry_pattern(backoff.expo, FacebookRequestError, max_tries=5, factor=5)
def do_request():
params = {'limit': RESULT_RETURN_LIMIT}
if self.current_bookmark:
params.update({'filtering': [{'field': 'ad.' + UPDATED_TIME_KEY, 'operator': 'GREATER_THAN', 'value': self.current_bookmark.int_timestamp}]})
yield self.account.get_ads(fields=self.automatic_fields(), params=params) # pylint: disable=no-member
@retry_pattern(backoff.expo, FacebookRequestError, max_tries=5, factor=5)
def do_request_multiple():
params = {'limit': RESULT_RETURN_LIMIT}
bookmark_params = []
if self.current_bookmark:
bookmark_params.append({'field': 'ad.' + UPDATED_TIME_KEY, 'operator': 'GREATER_THAN', 'value': self.current_bookmark.int_timestamp})
for del_info_filt in iter_delivery_info_filter('ad'):
params.update({'filtering': [del_info_filt] + bookmark_params})
filt_ads = self.account.get_ads(fields=self.automatic_fields(), params=params) # pylint: disable=no-member
yield filt_ads
@retry_pattern(backoff.expo, FacebookRequestError, max_tries=5, factor=5)
def prepare_record(ad):
return ad.api_get(fields=self.fields()).export_all_data()
if CONFIG.get('include_deleted', 'false').lower() == 'true':
ads = do_request_multiple()
else:
ads = do_request()
for message in self._iterate(ads, prepare_record):
yield message
示例14: test_get_delete_metric_descriptor
# 需要导入模块: import backoff [as 别名]
# 或者: from backoff import expo [as 别名]
def test_get_delete_metric_descriptor(capsys, custom_metric_descriptor):
try:
@backoff.on_exception(
backoff.expo, (AssertionError, NotFound), max_time=60)
def eventually_consistent_test():
snippets.get_metric_descriptor(custom_metric_descriptor)
out, _ = capsys.readouterr()
assert 'DOUBLE' in out
eventually_consistent_test()
finally:
snippets.delete_metric_descriptor(custom_metric_descriptor)
out, _ = capsys.readouterr()
assert 'Deleted metric' in out
示例15: test_custom_metric
# 需要导入模块: import backoff [as 别名]
# 或者: from backoff import expo [as 别名]
def test_custom_metric(client, custom_metric):
# Use a constant seed so psuedo random number is known ahead of time
random.seed(1)
pseudo_random_value = random.randint(0, 10)
INSTANCE_ID = "test_instance"
# It's rare, but write can fail with HttpError 500, so we retry.
@backoff.on_exception(backoff.expo, HttpError, max_time=120)
def write_value():
# Reseed it to make sure the sample code will pick the same
# value.
random.seed(1)
write_timeseries_value(client, PROJECT_RESOURCE,
METRIC_RESOURCE, INSTANCE_ID,
METRIC_KIND)
write_value()
# Sometimes on new metric descriptors, writes have a delay in being
# read back. Use backoff to account for this.
@backoff.on_exception(
backoff.expo, (AssertionError, HttpError), max_time=120)
def eventually_consistent_test():
response = read_timeseries(
client, PROJECT_RESOURCE, METRIC_RESOURCE)
# Make sure the value is not empty.
assert 'timeSeries' in response
value = int(
response['timeSeries'][0]['points'][0]['value']['int64Value'])
# using seed of 1 will create a value of 1
assert pseudo_random_value == value
eventually_consistent_test()