如何使全局变量在部署时不执行,或者引用它的最佳方法是什么?

2024-04-24 21:31:54 发布

您现在位置:Python中文网/ 问答频道 /正文

我在用chalice做Neo4j的应用。 我的(示例)代码如下所示:

from neo4j import GraphDatabase

uri = "bolt://<some IP address>:7687"
driver = GraphDatabase.driver(uri, auth=("neo4j", "<password>"))

def do_something(u):
    with driver.session() as session:
        result = session.run(
            "MATCH (u) "
            "RETURN u",
            u=u)

如您所见,我的'uri'和'driver'是在顶层声明的,当我需要为许多不同的函数引用它们时,这是非常理想的

当我执行chalice部署时,它会尝试解析生成/打包中的变量driver,这会导致数据库超时错误,可能是因为我并不总是让它运行。你知道吗

有没有更好的办法?你知道吗

我可以这样写,它将部署,但似乎没有必要复制这么多:

 def do_something(u):
    with GraphDatabase.driver(uri, auth=("neo4j", "<password>")).session() as session:
        result = session.run(
           "MATCH (u) "
           "RETURN u",
           u=u)

谢谢


Tags: runauthsessiondefasmatchdriverwith