使用 sqlalchemy 和 postgres 的 SSL 系统调用错误文件描述符错误 [英] SSL syscall error bad file descriptor using sqlalchemy and postgres

查看:36
本文介绍了使用 sqlalchemy 和 postgres 的 SSL 系统调用错误文件描述符错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我有一个通过 sqlalchemy 与 Postgres 对话的守护进程.守护进程做这样的事情:

So I have a daemon process that talks to Postgres via sqlalchemy. The daemon does something like this:

while True:
    oEngine = setup_new_engine()
    with oEngine.connect() as conn:
        Logger.debug("connection established")
        DBSession = sessionmaker(bind=conn)()
        Logger.debug('DBSession created. id={0}'.format(id(DBSession)))

        #do a bunch of stuff with DBSession

        DBSession.commit()
        Logger.debug('DBSession committed. id={0}'.format(id(DBSession)))

在永远循环的第一次迭代中,一切都很好.一阵子.DBSession 成功地对数据库进行了几次查询.但随后一个查询失败并出现错误:

On the first iteration of the forever loop everything works great. For a while. The DBSession successfully makes a few queries to the database. But then one query fails with the error:

OperationalError: (OperationalError) SSL SYSCALL error: Bad file descriptor

这告诉我正在使用关闭的连接或文件描述符.但是连接是由守护进程创建和维护的,所以我不知道这是什么意思.

This speaks to me of a closed connection or file descriptor being used. But the connections are created and maintained by the daemon so I don't know what this means.

换句话说,发生的是:

 create engine
 open connection
 setup dbsession
 query dbsession => works great
 query dbsession => ERROR

有问题的查询看起来像:

The query in question looks like:

 DBSession.query(Login)
                        .filter(Login.LFTime == oLineTime)
                        .filter(Login.success == self.success)
                        .count()

这对我来说似乎完全合理.

which seems perfectly reasonable to me.

我的问题是:这种行为可能有哪些原因,我该如何解决或隔离问题?

My question is: What kinds of reasons could there be for this kind of behaviour and how can I fix it or isolate the problem?

如果您需要更多代码,请告诉我.有很多东西,所以我在这里采用了极简主义的方法......

Let me know if you need more code. There is a heck of a lot of it so I went for the minimalist approach here...

推荐答案

我通过考虑会话范围而不是事务范围解决了这个问题.

I fixed this by thinking about the session scope instead of the transaction scope.

while True:
    do_stuff()

def do_stuff():
    oEngine = setup_new_engine()
    with oEngine.connect() as conn:
        Logger.debug("connection established")
        DBSession = sessionmaker(bind=conn)()

        #do a bunch of stuff with DBSession

        DBSession.commit()
        DBSession.close()

我仍然想知道为什么这个固定的东西...

I would still like to know why this fixed things though...

这篇关于使用 sqlalchemy 和 postgres 的 SSL 系统调用错误文件描述符错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆