首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >云函数遇到异常后无法继续运行

云函数遇到异常后无法继续运行
EN

Stack Overflow用户
提问于 2021-11-25 17:16:12
回答 1查看 72关注 0票数 1

所以我做了一个云函数,负责通过SFTP服务器读取文件,然后将它们推送到Google BigQuery。目录中一次有4-5个相关文件,有时会出错。为了弥补他们的错误,我在try-except块中读取它。它在本地工作,当脚本在Compute Engine实例上运行时(在我尝试将其移动到Cloud Functions之前,它被托管在那里。在Cloud Functions上,它在第一个文件遇到错误后停止运行。附件是日志截图以及。我已经尝试增加函数和分配给它的内存的时间限制,但是没有帮助。

整个脚本:

代码语言:javascript
复制
import pysftp
import json
import pandas as pd
from google.cloud import bigquery
def push_files():
  client = bigquery.Client()
  remote_path='/home/user/s/'
  cnopts = pysftp.CnOpts()
  cnopts.hostkeys = None    
  sftp=pysftp.Connection('host_ip',  private_key='private_key.pem' , username = 'user', cnopts=cnopts)
  files = [file for file in sftp.listdir(remote_path) if not file.__contains__('.ipynb')
        and not file.__contains__('sgp') and not file.__contains__('booking')
        and not file.__contains__('web') and not file.__contains__('SHIPPING')
        and not file.__contains__('addresses_v2') and not file.__contains__('fc_cust')]

  print(json.dumps(dict(
            severity="NOTICE",
            message=f"These are files to push {files}",)))

  for file in files:
    try:
      df = pd.read_csv(sftp.open(f"{remote_path}/{file}"), sep='\t', engine='python')
      table_id = f"sftp_data.{file}"
      job = client.load_table_from_dataframe(
          df, table_id
      )

      # Wait for the load job to complete.
      job.result()
      print(json.dumps(dict(
            severity="NOTICE",
            message=f"pushed {file}",)))
                        
    except Exception as e:
      print(json.dumps(dict(
            severity="NOTICE",
            message=f"could not push {file} because of {e.__str__()}",)))

  return "Pushed Files!"


def main(request):
  print('request: recieved')
  response = push_files()
  return f'Executed Request:{request} and {response}'

下面是云函数的日志:

EN

回答 1

Stack Overflow用户

发布于 2021-11-25 22:21:01

您需要一个continue来恢复循环

代码语言:javascript
复制
for file in files:
    try:
      df = pd.read_csv(sftp.open(f"{remote_path}/{file}"), sep='\t', engine='python')
      table_id = f"sftp_data.{file}"
      job = client.load_table_from_dataframe(
          df, table_id
      )

      # Wait for the load job to complete.
      job.result()
      print(json.dumps(dict(
            severity="NOTICE",
            message=f"pushed {file}",)))
                        
    except Exception as e:
      print(json.dumps(dict(
            severity="NOTICE",
            message=f"could not push {file} because of {e.__str__()}",)))
      continue
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/70115077

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档