为了账号安全,请及时绑定邮箱和手机立即绑定

如何在特定的“完成”状态下获得工作

如何在特定的“完成”状态下获得工作

有只小跳蛙 2021-11-09 18:27:25
我正在尝试确定各种工作状态。Bigquery 提供了我知道的三种状态:DONE、PENDING 和 RUNNING。但是,我正在尝试根据以下内容获取状态:完毕待办的成功的错误取消跑步我将如何以不太“昂贵”的方式执行此操作,因为我正在以一种“长轮询”方式迭代大约 100 个结果,大约每十秒一次。目前我正在做类似的事情:jobs = [job for job in self.bq_client.list_jobs(project=PROJECT_ID]if state is not None:    jobs = [job for job in jobs if job.state == state]如果状态为 DONE、RUNNING 或 PENDING 之一,则上述方法有效。但是我将如何覆盖其他州?
查看完整描述

1 回答

?
www说

TA贡献1775条经验 获得超8个赞

状态跟踪作业进度,如果你需要成功/失败,你要考虑的信息errorResult中的响应。对于成功的工作,这将是None,对于取消的工作,您将得到{u'reason': u'stopped', u'message': u'Job execution was cancelled: User requested cancellation'}。我用来测试这个的代码:


from google.cloud import bigquery

client = bigquery.Client()


project = "[PROJECT-ID]"

states = ["RUNNING", "PENDING", "SUCCESSFUL", "CANCELLED", "FAILED"]



def returnState(job):

  if job.state == "DONE":

    if job.error_result is None:

      return "SUCCESSFUL"

    elif job.error_result['reason'] == u'stopped':

      return "CANCELLED"

    else:

      return "FAILED"

  else:

    return job.state



jobs = [job for job in client.list_jobs(project=project, max_results=10)]


for state in states:

  matching_jobs = [job for job in jobs if returnState(job) == state]


  for job in matching_jobs:

    print "Job ID: {0}, State: {1}, Error Result: {2}".format(job.job_id, state, job.error_result)

这将打印出如下内容:


$ python bq-status.py

Job ID: bquijob_..., State: SUCCESSFUL, Error Result: None

Job ID: bquijob_..., State: SUCCESSFUL, Error Result: None

Job ID: job_..., State: SUCCESSFUL, Error Result: None

Job ID: job_..., State: SUCCESSFUL, Error Result: None

Job ID: job_..., State: SUCCESSFUL, Error Result: None

Job ID: job_..., State: SUCCESSFUL, Error Result: None

Job ID: scheduled_query_..., State: SUCCESSFUL, Error Result: None

Job ID: bquijob_..., State: SUCCESSFUL, Error Result: None

Job ID: bquijob_..., State: CANCELLED, Error Result: {u'reason': u'stopped', u'message': u'Job execution was cancelled: User requested cancellation'}

Job ID: bquijob_..., State: FAILED, Error Result: {u'reason': u'invalidQuery', u'message': u'Syntax error: Illegal input character "\\\\" at [2:18]', u'location': u'query'}

请记住,负载的工作可能是成功的,但允许一些maxBadRecords,这样errorResult将不为空,等等。


查看完整回答
反对 回复 2021-11-09
  • 1 回答
  • 0 关注
  • 196 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信