1

一部のモデルデータからリストを作成していますが、正しく実行していません。正しく機能しますが、ブラウザのページを更新すると、reportResultsが追加されます。リクエストの合間にガベージコレクションが行われることを望んでいましたが、明らかに私は何か間違ったことをしています。

ありがとう、Ewan

reportResults = []   #the list that doesn't get collected
def addReportResult(fix,description):  
    fix.description = description
    reportResults.append(fix)

def unitHistory(request,unitid, syear, smonth, sday, shour, fyear, fmonth, fday, fhour, type=None):
   waypoints = Fixes.objects.filter(name=(unitid))
    waypoints = waypoints.filter(gpstime__range=(awareStartTime, awareEndTime)).order_by('gpstime')[:1000]
    if waypoints:
        for index in range(len(waypoints)): 
...do stuff here selecting some waypoints and generating "description" text
                    addReportResult(waypointsindex,description) ##append the list with this, adding a text description

    return render_to_response('unitHistory.html', {'fixes': reportResults})   
4

2 に答える 2

1

毎回同じリストを再利用しています。これを修正するには、コードを再構築して、リクエストごとに新しいリストを作成する必要があります。これは複数の方法で行うことができ、これはそのような方法の1つです。

def addReportResult(reportResults, fix,description):  
    fix.description = description
    reportResults.append(fix)

def unitHistory(request,unitid, syear, smonth, sday, shour, fyear, fmonth, fday, fhour, type=None):

    reportResults = [] # Here we create our local list that is recreated each request.

    waypoints = Fixes.objects.filter(name=(unitid))
    waypoints = waypoints.filter(gpstime__range=(awareStartTime, awareEndTime)).order_by('gpstime')[:1000]
    if waypoints:
        for index in range(len(waypoints)):
            # Do processing
            addReportResult(reportResults, waypointsindex, description)
            # We pass the list to the function so it can use it.

return render_to_response('unitHistory.html', {'fixes': reportResults})

小さいままの場合は、への呼び出しを完全に削除し、同じ位置でを実行することによりaddReportResult、属性セットをインライン化することもできます。descriptionaddReportResultwaypointsindex.description = description

于 2013-01-19T10:29:39.513 に答える
0

リクエストのライフサイクルを知っているのと同じように、mod_wsgiはプロセスを開いたままにして複数のリクエストを処理します。そのプロセスは頻繁にリサイクルされますが、あなたが想定しているように、それは間違いなく単一の要求に拘束されません。

つまり、ローカルリストが必要です。関数の内容を直接インラインで移動することをaddReportResultお勧めしますが、再利用可能にする必要がある場合、または関数が長すぎる場合は、それは良い考えではありません。代わりに、その関数でアイテムを返すようにします。そうすれば、結果をローカルで収集できます。

def create_report(fix, description): # I've changed the name to snake_casing
    fix.description = description
    return fix

def unit_history(request,unitid, syear, smonth, sday, shour, fyear, fmonth, fday, fhour, type=None):
    reports = []
    waypoints = Fixes.objects.filter(name=(unitid))
    waypoints = waypoints.filter(gpstime__range=(awareStartTime, awareEndTime)).order_by('gpstime')[:1000]
    if waypoints:
        for index in range(len(waypoints)): 
            report = create_report(waypointsindex, description)
            reports.append(report)
    return render_to_response('unitHistory.html', {'fixes': reportResults})
于 2013-01-19T10:45:45.707 に答える