1
我正在運行一個子進程,它使用Django視圖中的python multiprocessing API在後臺執行一些繁重的處理。 這對sqlite很好,但在移動到PostgreSQL時崩潰。我認爲它與兩個進程中的一個數據庫連接有關。 正在關注solution to double connection我試圖在啓動過程之前以及過程中關閉連接。在Django中,如何刷新子進程的數據庫連接
這些更改消除了崩潰,但導致無法將數據保留在數據庫中。我認爲要麼沒有重新連接,要麼連接有問題。
這裏是有關代碼調用過程:
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();