Python django.db连接从psycopg2返回不同的值

Python django.db连接从psycopg2返回不同的值,python,django,psycopg2,Python,Django,Psycopg2,背景:我正在使用进程将从django db获取值的python脚本转换为多线程。 我可能错过了django中的一些非常新的东西,但得到了DatabaseError:SSL错误:解密失败或错误记录 所以我已经根据…关闭了连接。。。但没有得到DatabaseError:SSL连接已意外关闭 我决定尝试连接psycopg2并关闭django版本位置的连接。 一切都很顺利 问题:当我测试解决方案时,我看到同一个查询返回不同的值 我的问题很简单: 从表中选择不同的someId,其中date

背景:我正在使用进程将从django db获取值的python脚本转换为多线程。 我可能错过了django中的一些非常新的东西,但得到了DatabaseError:SSL错误:解密失败或错误记录 所以我已经根据…关闭了连接。。。但没有得到DatabaseError:SSL连接已意外关闭

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

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

我的问题很简单: 从表中选择不同的someId,其中date<'date'in'u the'u the'u past'

请给我一些建议

更新: 我无法显示完整的代码。。我更改了名称等,但流量100%相同。 lanchAll方法为每个t_id启动进程,当我使用django.db连接搜索时,我得到3个不同的值,当使用psycopg2进行搜索时,我得到1个值。 在运行时,这将通过In_sim调用,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)

我怀疑您在创建连接后正在分叉。Libpq连接psycopg使用的连接


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

添加您正在使用的代码可能是事务问题,每个进程都是原子的?正如@kroolik所说的,代码是必需的。谢谢你们,我已经添加了有问题的代码。谢谢你们的输入,我会试试的。