简体   繁体   English

SQLAlchemy create_engine 在 Google Cloud Function 中不起作用

[英]SQLAlchemy create_engine not working in Google Cloud Function

I am trying to run a Google Cloud Function that collects data off a website then inserts it into a Cloud SQL (MySQL) database, but having problems with SQLAlchemy in Cloud which don't appear on my local machine.我正在尝试运行从网站收集数据的 Google Cloud Function,然后将其插入 Cloud SQL (MySQL) 数据库,但在 Cloud 中使用 SQLAlchemy 时遇到了问题,这些问题不会出现在我的本地机器上。 Any suggestions!?有什么建议!?

When I run the function locally, against Py3.7 (on a Mac, not using virtualenv), using the Cloud SQL Proxy and SQLAlchemy, I successfully connect to the database.当我在本地运行该函数时,针对 Py3.7(在 Mac 上,不使用 virtualenv),使用 Cloud SQL 代理和 SQLAlchemy,我成功连接到数据库。

When running the Cloud Function, I use connection string in this format mysql+pymysql://<username>:<password>/<dbname>?unix_socket=/cloudsql/<PROJECT-NAME>:<INSTANCE-REGION>:<INSTANCE-NAME> .运行云函数时,我使用这种格式的连接字符串mysql+pymysql://<username>:<password>/<dbname>?unix_socket=/cloudsql/<PROJECT-NAME>:<INSTANCE-REGION>:<INSTANCE-NAME> .

The Cloud Function keeps throwing the following exception for SQLAlchemy.create_engine.云函数不断为 SQLAlchemy.create_engine 抛出以下异常。 It does not appear to be related to being able to connect, but due to instantiation.它似乎与能够连接无关,而是由于实例化。

Everything is in the same project.一切都在同一个项目中。

I have also tried using the public IP and connection string in the format mysql+pymysql://<username>:<password>@<public ip address>:3306/<dbname> , which made no difference.我还尝试使用mysql+pymysql://<username>:<password>@<public ip address>:3306/<dbname>格式的公共 IP 和连接字符串,这没有区别。

Traceback (most recent call last):
  File "/env/local/lib/python3.7/site-packages/google/cloud/functions/worker_v2.py", line 449, in run_background_function
    _function_handler.invoke_user_function(event_object)
  File "/env/local/lib/python3.7/site-packages/google/cloud/functions/worker_v2.py", line 268, in invoke_user_function
    return call_user_function(request_or_event)
  File "/env/local/lib/python3.7/site-packages/google/cloud/functions/worker_v2.py", line 265, in call_user_function
    event_context.Context(**request_or_event.context))
  File "/user_code/main.py", line 14, in retrieve_and_log
    engine = create_engine(connection_string,echo=True)
  File "/env/local/lib/python3.7/site-packages/sqlalchemy/engine/__init__.py", line 500, in create_engine
    return strategy.create(*args, **kwargs)
  File "/env/local/lib/python3.7/site-packages/sqlalchemy/engine/strategies.py", line 56, in create
    plugins = u._instantiate_plugins(kwargs)
AttributeError: 'Context' object has no attribute '_instantiate_plugins'

Here is a snippet of my code:这是我的代码片段:

import requests
from bs4 import BeautifulSoup
from sqlalchemy import create_engine, MetaData, Table, Column, Integer, String

def retrieve_and_log(store_string, connection_string = 'mysql+pymysql://<username>:<password>/<dbname>?unix_socket=/cloudsql/<PROJECT-NAME>:<INSTANCE-REGION>:<INSTANCE-NAME>'):

    engine = create_engine(connection_string,echo=True)
    
    conn = engine.connect()
    # ....


If retrieve_and_log is the function you are trying to deploy as a background Cloud Function, it needs a signature like:如果retrieve_and_log是您尝试部署为后台云函数的函数,则它需要如下签名:

def retrieve_and_log(data, context):
    ...

It can't take arbitrary parameters.它不能接受任意参数。

See https://cloud.google.com/functions/docs/writing/background for more details.有关更多详细信息,请参阅https://cloud.google.com/functions/docs/writing/background

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM