【问题标题】:Google Cloud Function Throwing Weird Error谷歌云函数抛出奇怪的错误
【发布时间】:2019-06-12 04:09:21
【问题描述】:

这里有人熟悉 Google Cloud Functions 吗?我阅读了他们的文档,并在此基础上定制了我的脚本以尝试在他们的托管环境中工作。

https://cloud.google.com/functions/docs/concepts/python-runtime

所以,我的 Python 脚本如下所示。

def main():

    requests
    numpy
    pandas
    datetime
    requests
    pandas_gbq
    xml.etree.ElementTree


    # authentication: working....
    login = 'my_email' 
    password = 'my_password'


    AsOfDate = datetime.datetime.today().strftime('%m-%d-%Y')

    #step into URL
    REQUEST_URL = 'https://www.business.com/report-api/device=779142&rdate=Yesterday'
    response = requests.get(REQUEST_URL, auth=(login, password))
    xml_data = response.text.encode('utf-8', 'ignore') 

    #tree = etree.parse(xml_data)
    root = xml.etree.ElementTree.fromstring(xml_data)

    # start collecting root elements and headers for data frame 1
    desc = root.get("Description")
    frm = root.get("From")
    thru = root.get("Thru")
    loc = root.get("locations")
    loc = loc[:-1]
    df1 = pandas.DataFrame([['From:',frm],['Through:',thru],['Location:',loc]])
    df1.columns = ['S','Analytics']
    #print(df1)

    # start getting the analytics for data frame 2
    data=[['Goal:',root[0][0].text],['Actual:',root[0][1].text],['Compliant:',root[0][2].text],['Errors:',root[0][3].text],['Checks:',root[0][4].text]]
    df2 = pandas.DataFrame(data)
    df2.columns = ['S','Analytics']
    #print(df2)

    # merge data frame 1 with data frame 2
    df3 = df1.append(df2, ignore_index=True)
    #print(df3)

    # append description and today's date onto data frame
    df3['Description'] = desc
    df3['AsOfDate'] = AsOfDate


    # push from data frame, where data has been transformed, into Google BQ
    pandas_gbq.to_gbq(df3, 'Metrics', 'analytics', chunksize=None, reauth=False, if_exists='append', private_key=None, auth_local_webserver=False, table_schema=None, location=None, progress_bar=True, verbose=None)
    print('Execute Query, Done!!')

main()

if __name__ == '__main__':
    main()  

另外,我的 requirements.txt 看起来像这样。

requests
numpy
pandas
datetime
requests
pandas_gbq
xml.etree.ElementTree

我的脚本在过去 2 个月内一直运行良好,但我需要每天在笔记本电脑上运行它。为了摆脱这个手动过程,我试图让它在云上运行。问题是我不断收到一条错误消息:TypeError: main() takes 0 positional arguments but 1 was given

对我来说,似乎没有给出任何参数,也没有预期的参数,但不知何故,谷歌说给出了 1 个参数。我可以稍微修改我的代码以使其正常工作,或者以某种方式绕过这个看似良性的错误吗?谢谢。

【问题讨论】:

    标签: python python-3.x google-cloud-platform google-cloud-functions


    【解决方案1】:

    您误解了 Cloud Functions 的工作原理。它不允许您简单地运行任意脚本。您编写触发器来响应 HTTP 请求,或者当您的 Cloud 项目发生变化时。这似乎不是你在这里做的。 Cloud Functions 部署不使用 main()。

    您可能需要阅读 overview documentation 以了解 Cloud Functions 的用途。

    如果您尝试定期运行某些东西,请考虑编写一个 HTTP 触发器,并让某些类似 cron 的服务以您想要的速率调用它。

    【讨论】:

    【解决方案2】:

    以下内容会获取您的代码并将其更改为使用 HTTP 触发器在 Google Cloud Functions 中运行。然后,您可以使用 Google Cloud Scheduler 按时调用您的函数。您还需要使用需要导入的模块创建一个requirements.txt。请参阅此document 了解更多信息。

    def handler(request):
    
        import requests
        import numpy
        import pandas
        import datetime
        import requests
        import pandas_gbq
        import xml.etree.ElementTree
    
    
        # authentication: working....
        login = 'my_email' 
        password = 'my_password'
    
    
        AsOfDate = datetime.datetime.today().strftime('%m-%d-%Y')
    
        #step into URL
        REQUEST_URL = 'https://www.business.com/report-api/device=779142&rdate=Yesterday'
        response = requests.get(REQUEST_URL, auth=(login, password))
        xml_data = response.text.encode('utf-8', 'ignore') 
    
        #tree = etree.parse(xml_data)
        root = xml.etree.ElementTree.fromstring(xml_data)
    
        # start collecting root elements and headers for data frame 1
        desc = root.get("Description")
        frm = root.get("From")
        thru = root.get("Thru")
        loc = root.get("locations")
        loc = loc[:-1]
        df1 = pandas.DataFrame([['From:',frm],['Through:',thru],['Location:',loc]])
        df1.columns = ['S','Analytics']
        #print(df1)
    
        # start getting the analytics for data frame 2
        data=[['Goal:',root[0][0].text],['Actual:',root[0][1].text],['Compliant:',root[0][2].text],['Errors:',root[0][3].text],['Checks:',root[0][4].text]]
        df2 = pandas.DataFrame(data)
        df2.columns = ['S','Analytics']
        #print(df2)
    
        # merge data frame 1 with data frame 2
        df3 = df1.append(df2, ignore_index=True)
        #print(df3)
    
        # append description and today's date onto data frame
        df3['Description'] = desc
        df3['AsOfDate'] = AsOfDate
    
    
        # push from data frame, where data has been transformed, into Google BQ
        pandas_gbq.to_gbq(df3, 'Metrics', 'analytics', chunksize=None, reauth=False, if_exists='append', private_key=None, auth_local_webserver=False, table_schema=None, location=None, progress_bar=True, verbose=None)
        # print('Execute Query, Done!!')
    
        # Normally for an HTTP trigger you would return a full HTML page here
        # <html><head></head><body>you get the idea</body></html>
        return 'Execute Query, Done!!'
    

    【讨论】:

    • 谢谢。我刚刚插入它并收到此错误消息:'部署失败:构建失败:{“error”:{“canonicalCode”:“INVALID_ARGUMENT”,“errorMessage”:“pip_download_wheels有stderr输出:\n找不到满足 xml.etree.ElementTree 要求的版本(来自 -r requirements.txt(第 6 行))(来自版本:)\n没有找到 xml.etree.ElementTree 的匹配分布(来自 -r requirements.txt(第 6 行)) \n\nerror: pip_download_wheels returned code: 1", "errorType": "InternalError", "errorId": "4B6915C4"}}' 我今天早些时候看到过几次。
    • 我没有检查您的导入语句。该错误表示 Cloud Functions 无法解析包 xml。通过pylint 运行您的代码并修复所有警告和错误。这还将仔细检查您的导入语句。
    • 我就是这么想的。这似乎是一种新产品,因此非常不成熟。当然,我可以想出一个解决方法。我期待这个东西比现在更多。感谢您的帮助!
    • xml.etree.ElementTreenot a valid package on PyPI,它是一个标准库模块:docs.python.org/3/library/xml.html
    猜你喜欢
    • 2020-05-01
    • 1970-01-01
    • 1970-01-01
    • 2012-07-23
    • 2021-12-31
    • 2017-12-23
    • 2023-03-22
    • 2017-07-25
    • 2018-05-11
    相关资源
    最近更新 更多