django.db.connection不支持fileno()方法

时间:2018-12-31 11:43:53

标签: django django-database

我在模型管理器上有一个自定义方法,该方法允许我使用postgreSQL来监听来自数据库修改的通知。此代码的简短版本如下所示:

def listen_for_notify(self):
        import select
        import psycopg2
        import psycopg2.extensions

        from django.conf import settings

        db_data = settings.DATABASES['default']
        listened = None
        returned_empty = None
        search_timeout = 15

        conn = psycopg2.connect(dbname=db_data['NAME'], user=db_data['USER'], password=db_data['PASSWORD'], host=db_data['HOST'], port=db_data['PORT'])
        conn.set_isolation_level(psycopg2.extensions.ISOLATION_LEVEL_AUTOCOMMIT)
        curs = conn.cursor()
        curs.execute("LISTEN default;")     

        timeout = timezone.now() + timezone.timedelta(0, search_timeout)
        while timezone.now() < timeout:
            time_diff = timeout - timezone.now()
            if select.select([conn], [], [], float(time_diff.seconds)) == ([], [], []):
                listened = False
                timeout = timezone.now()
            else:
                conn.poll()
                while conn.notifies:
                    notify = conn.notifies.pop(0)
                    if notify.payload == "notified":
                        listened = True
                        returned_empty = False
                        timeout = timezone.now()
                    if notify.payload == 'search request returned empty':
                        listened = True
                        returned_empty = True
                        timeout = timezone.now()
        curs.close()
        conn.close()
        return listened, returned_empty

如果我只能使用django.db而不是使用psycopg2库,那将非常好。像这样:

def listen_for_notify(self):
        from django.db import connection as conn

        listened = None
        returned_empty = None
        search_timeout = 15

        with conn.cursor() as curs
            timeout = timezone.now() + timezone.timedelta(0, search_timeout)
            while timezone.now() < timeout:
                time_diff = timeout - timezone.now()
                if select.select([conn], [], [], float(time_diff.seconds)) == ([], [], []):
                    listened = False
                    timeout = timezone.now()
                else:
                    conn.poll()
                    while conn.notifies:
                        notify = conn.notifies.pop(0)
                        if notify.payload == "notified":
                            listened = True
                            returned_empty = False
                            timeout = timezone.now()
                        if notify.payload == 'search request returned empty':
                            listened = True
                            returned_empty = True
                            timeout = timezone.now()
        return listened, returned_empty

我使用django.db尝试了上述解决方案,但由于django.db.connection对象没有fileno()方法而无法使用。

当前是否不支持此功能,或者我缺少某些功能?尽管django.db只是实际psycopg2库的包装。所以我想知道为什么我不能在其上使用fileno()方法。

0 个答案:

没有答案