在Django中,如何刷新子进程的数据库连接

时间:2014-04-13 13:16:21

标签: python django

我正在使用Django视图中的python多处理API运行子进程,以在后台执行一些繁重的处理。 这适用于sqlite,但在转移到PostgreSQL时崩溃了。我认为它与在两个进程中建立一个数据库连接有关。 在solution to double connection之后,我尝试在启动流程之前以及流程内部关闭连接。

这些更改消除了崩溃,但导致无法将数据保留在数据库中。我认为要么没有重新连接,要么连接有点错误。

这是调用该过程的相关代码:

    db.close_connection()
    process = multiprocessing.Process(target=Util.processImage, args=(processUtil, img_full_path, snapshot.pk, process_pk))
    process.start()

这是流程中的代码:

import sys, traceback, json

from django.db import connection

from datetime import datetime

from yeast_libraries.models import YeastPlateStack_Model, PlateSnapshot_Model, LocusAnalysis_Model,\
    StorageLocation_Model, YeastLibrary_Model, SnapshotProcess_Model

from mediums.models import Batch_Model


from image_analysis.image_processor import ImageAnalysisControler

from yeast_libraries import db_manager


class Util:

def processImage(self, img_full_path, snapshot_pk, process_pk):



    print('process_pk: ', process_pk)



    try:
        snapshot_process = SnapshotProcess_Model.objects.get(pk=process_pk)
        print(snapshot_process.status)

        status = 'bussy'

        print('processImage:', datetime.now())
        sys.stdout.flush()


        snapshot = PlateSnapshot_Model.objects.get(pk = snapshot_pk)
        imageAnalysisControler = ImageAnalysisControler()
    #             print('a:', a)

        processed_image_path = img_full_path[:-5] + '_p.jpg'

        global grid

        print('process image: getting analysis')



        #grid = imageAnalysisControler.analyzeYeastPlateImage(img_full_path, processed_image_path)
        grid = imageAnalysisControler.analyzeYeastPlateImage('image_analysis/plates/384_0001.jpg', processed_image_path)

        print('process image: got analysis')

        snapshot.processed_image_path = processed_image_path

        for cell in grid['grid']:

            locus = LocusAnalysis_Model.objects.create(
                area_scaled = cell['area_scaled'], 
                is_empty = cell['is_empty'], 
                column = cell['column'],  
                row = cell['row'], 
                ratio = cell['ratio'], 
                center_x = cell['center_x'], 
                center_y = cell['center_y']
            )
            snapshot.analysis.add(locus)

        snapshot.save()

        sys.stdout.flush()

        status = 'completed' 

    except Exception:
        print('exception: ', sys.exc_info)
        traceback.print_exc()
        status = 'failed' 

    snapshot_process.status = status
    snapshot_process.save();   

1 个答案:

答案 0 :(得分:0)

在尝试了几件事之后,我决定使用psycopg2绑定为python进行解决,并使用了完全不同的连接和SQL