【问题标题】:django.db connection return different values from psycopg2django.db 连接从 psycopg2 返回不同的值
【发布时间】:2014-09-29 14:47:27
【问题描述】:

背景:我正在使用 Process 将从 django-db 获取值的 python 脚本转换为多线程。 我可能错过了一些东西(在 django 中很新)但得到“DatabaseError:SSL 错误:解密失败或坏记录 mac” 所以我已经关闭了连接(根据this)......但是得到了“DatabaseError:SSL连接已意外关闭”。

我决定尝试 psycopg2 连接并关闭 django 版本位置的连接。 一切都很好。

问题:当我测试解决方案时,我发现同一个查询返回不同的值!

我的查询很简单: "SELECT DISTINCT "someId" FROM aTable WHERE "date"

请指教..

更新: 我无法显示完整的代码。我已经更改了名称等,但流程是 100% 相同的。 lanchAll 方法为每个 t_id 启动进程,当我使用 django.db 连接搜索时,我得到 3 个不同的值,当使用 psycopg2 进行搜索时,我得到 1 个值。 在运行时,这是用 in_sim 调用的,它是过去的日期。 在 lanchAll 中,django.db 连接被标记为 psycopg2 旁边的注释...

from multiprocessing import Process
import sys
from numpy import *
from itertools import izip_longest
from datetime import * 
import psycopg2
from appName import settings
from django.core.management import setup_environ
from django.core.exceptions import ObjectDoesNotExist

import argparse
import pdb
setup_environ(settings)

from appName.models import *
from appName.aClass import *
from django.db import connection

def lanchAll(in_sim):
    p = 0
    try:
        con = psycopg2.connect(database=DB_NAME, user=DB_USER, password=DB_PASS,
                        host=DB_IP)
        cursor = con.cursor()
        # cursor = connection.cursor()
        if in_simolationDate is None:
            query = 'SELECT DISTINCT "t_id" FROM appName_tableNAme '\
                'WHERE "date" > now() - interval \'%s seconds\';'        
            cursor.execute(query, [TIME_WINDOW])    
        else:
            query = 'SELECT DISTINCT "t_id" FROM appName_tableNAme '\
                'WHERE "date" < %s ;'        
            cursor.execute(query, [in_sim])    

    except ObjectDoesNotExist as e:
        con.close()
        # connection.close()
        print((str('DB Error: %s\n' % e)))
        return -3
    if cursor.rowcount < 1:
        con.close()
        # connection.close()
        print ("offline!")
        return -4

    for row in onlineTagsCursor:
        t_id = row[0]   
        print "currently lunching tagId:" + str(t_id)
        processInstance = Process(target=launchThread, args=(t_id, p,in_sim))
        processInstance.start()

    con.close()
    # connection.close()


def launcher(in_id, in_p,in_sim):
    #I'll add it if you fell that it is needed.. 


#the model for the table     
class tableNAme(models.Model):
    m_id    = models.IntegerField()
    sc_id   = models.IntegerField()
    t_id    = models.IntegerField()
    r       = models.IntegerField()
    l       = models.IntegerField()
    g       = models.IntegerField(0)
    p       = models.IntegerField(0)
    date    = models.DateTimeField(auto_now=True)

【问题讨论】:

  • 添加你正在使用的代码
  • 可能是事务问题,每个进程都是原子的?正如@kroolik 所说,需要代码。
  • 谢谢两位,我已经添加了有问题的代码。

标签: python django psycopg2


【解决方案1】:

我怀疑你是在创建连接后分叉的。 Libpq 连接(psycopg 使用的连接)don't support fork

确保新进程创建自己的连接,它们不能使用父进程创建的连接。

【讨论】:

  • 感谢您的意见,我会试试的。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-10-05
  • 1970-01-01
  • 2011-07-22
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多