0

我最近从本地 web.py/apache 设置转移到共享主机,我正在尝试匹配我的家庭配置。出现的一个问题是 OperationalError "MySQL server has gone away"。在互联网上搜索,遇到此错误的人往往会在几个小时内不活跃。这在几秒钟之间发生在我身上。

我已经使用 mod_wsgi 的 application() 函数示例确认我实际上是在守护进程模式下运行的。不过,我担心的一个问题是,如果我将 web.ctx.orm 吐到错误日志中,它似乎是每个请求的新对象。我的 sqlalchemy 会话对象在页面请求之间不应该相同吗?

这是我的 python 代码和我的 apache 设置的一部分。在这台新机器上,我以前在家用机器上没有遇到过什么问题吗?

def load_sqla(handler):
    web.ctx.orm = scoped_session(sessionmaker(bind=engine))
    try:
        try:
            return handler()
        except web.HTTPError:
            web.ctx.orm.commit()
            raise
        except:
            web.ctx.orm.rollback()
            raise
    finally:
        web.ctx.orm.commit()
        # If the above alone doesn't work, uncomment
        # the following line:
        web.ctx.orm.expunge_all()

... urls and controllers ...

app = web.application(urls, globals(), autoreload=False)
app.add_processor(load_sqla)
application = app.wsgifunc()

这是我的 apache 设置的一部分。

WSGIDaemonProcess app processes=1 threads=1 python-path=/home/net/
public_html/myapp
WSGIProcessGroup app
WSGIScriptAlias /myapp /home/net/public_html/myapp/managio.py
<Directory "/home/stratton/public_html/myapp">
 Options Indexes MultiViews FollowSymLinks
 AllowOverride None
 Order allow,deny
 allow from all
</Directory> 
4

1 回答 1

1

检查文档:http ://code.google.com/p/modwsgi/wiki/ConfigurationDirectives#WSGIDaemonProcess

设置processes=1实际上使多处理保持打开状态,这可能就是您获得对同一 sql 连接的并发访问的原因。

此外,您似乎正在使用 SQLAlchemy,所以也许在您制作引擎时尝试打开 QueuePool 或 NullPool 使用?

于 2011-02-23T22:34:05.320 回答