首页 > 解决方案 > TypeError:“上下文”对象不可用于谷歌云功能

问题描述

有人可以帮助诊断和纠正问题所在,因为此错误一直引用“上下文”对象(我在理论上将 schema_df 解释为该对象)?

我一直在尝试部署一个在以下部分本地工作的云功能。

def convert_schema(results_df, schema_df):
    """Converts data types in dataframe to match BigQuery destination table"""
    dict(schema_df)
    print(schema_df)
    for k in schema_df: #for each column name in the dictionary, convert the data type in the dataframe
        results_df[k] = results_df[k].astype(schema_df.get(k))
    results_df_transformed = results_df
    print("Updated schema to match BigQuery destination table")
    return results_df_transformed

schema_df = {'_comments': 'object', 
                            '_direction': 'object', 
                            '_fromst': 'object', 
                            '_last_updt': 'datetime64',          
                            '_length': 'float64', 
                            '_lif_lat': 'float64', 
                            '_lit_lat': 'float64', 
                            '_lit_lon': 'float64',
                            '_strheading': 'object', 
                            '_tost': 'object', 
                            '_traffic': 'int64', 
                            'segmentid': 'int64', 
                            'start_lon': 'float64', 
                            'street': 'object'
              }

但是,它不起作用,也不会将传入的对象识别为可迭代字典。

                                                              File "/env/local/lib/python3.7/site-packages/google/cloud/functions/worker.py", line 349, in run_background_function
                                                                _function_handler.invoke_user_function(event_object)
                                                              File "/env/local/lib/python3.7/site-packages/google/cloud/functions/worker.py", line 215, in invoke_user_function
                                                                return call_user_function(request_or_event)
                                                              File "/env/local/lib/python3.7/site-packages/google/cloud/functions/worker.py", line 212, in call_user_function
                                                                event_context.Context(**request_or_event.context))
                                                              File "/user_code/main.py", line 43, in handler
                                                                results_df_transformed = convert_schema(results_df, schema_df)
                                                              File "/user_code/lib/data_ingestion.py", line 84, in convert_schema
                                                                dict(schema_df)
                                                            TypeError: 'Context' object is not iterable

标签: python-3.xdictionarygoogle-cloud-platformgoogle-cloud-functions

解决方案


响应事件的函数(即“后台函数”)需要具有签名:

def my_function(data, context):
    ...

Cloud Functions 运行时在每个新事件上提供的位置data和位置。context

您可能希望convert_schema在顶级后台函数中调用您的函数。有关详细信息,请参阅“编写后台函数”。


推荐阅读