참고 : 죄송 합니다만 두 개 이상의 링크를 게시하는 데 필요한 평판 점수가 없습니다. 아래에 게시 된 스 니펫은 views 및 지원되는 클래스 '__del__() method을 참조하십시오.Django의 세션 미들웨어를 사용한 가비지 콜렉션
나는 클래스의 인스턴스를 장고 세션의 값으로 저장하는 장고에서 함수 기반 뷰를 가지고있다.
# First view Function
if request.method == 'POST':
logger.info('User %s Began Upload of File %s' % (request.user.username, request.FILES['file']))
form = uploadFileForm1(request.POST, request.FILES)
if form.is_valid():
# form.cleaned_data contains an in memory version of the uploaded file.
uploaded_shp = ShpUploader(form.cleaned_data['file'])
# Store ShpUploader instance in cookie to be referenced
request.session['uploaded_shp'] = uploaded_shp
return HttpResponseRedirect('./2')
else:
for uploadfile_error in form.errors['file']:
logger.warning(uploadfile_error)
세션은 나중의보기에서 액세스됩니다.이 개체의 일부 메소드는 세션 내에 저장됩니다.
# Second view Function
if request.method == 'POST':
# Required to repass shpPath kwarg
form = uploadFileForm2(request.POST,shpPath=request.session['uploaded_shp'].upload_full_path)
if form.is_valid():
# Pass user-defined field mappings to import_shapefile method.
request.session['uploaded_shp'].import_shapefile(form.cleaned_data)
logger.info('Successful - User %s Uploaded File %s' % (request.user.username, request.session['uploaded_shp'].upload_full_path))
return HttpResponseRedirect('./success')
else:
print form.errors
내가 처음에 내 수업에 __del__()
메서드를 재정의 아이디어 장난 삼아 생각해했다가 자동으로 폴더이 객체 참조를 삭제합니다.
# Inside my class definition
def __del__(self):
"""
Given a directory, remove it an its contents.
Intended to clean up temporary files after upload.
"""
shutil.rmtree(self.upload_dir)
logger.info('Delete Successful: %s' % self.upload_dir)
내 문제는 내 수업 '__del__()
방법은 세션 내에서 실제 개체를 저장에도 불구하고, 첫 번째보기와 두 번째보기 사이에 실행되는 이유입니까?
사용자 정의 __del__()
메서드가있는 클래스를 사용하여 기본 예제를 작성하려고했지만 사전을 사용하여 함수간에 개체를 유지합니다. 이 예제 :
class tester(object):
def __init__(self, val):
self.val = val
print 'created in %s' % self.val
def __del__(self):
print 'deleted'
cache = {}
def f1():
print 'in f1'
t = tester('Test Object Value')
cache['tdict'] = t
print cache['tdict'].val
def f2():
print 'in f2'
print cache['tdict'].val
if __name__ == '__main__':
f1()
f2()
에만 두 번째 기능 f2()
를 종료 한 후 I가 __del__()
메소드를 호출, 예상했을 것이다 넣어 출력을 생성합니다.
in f1
created in Test Object Value
Test Object Value
in f2
Test Object Value
deleted
[Finished in 0.1s]
세션 미들웨어의 작동 방식에 관한 내용이 누락 되었습니까? 한 가지 생각은 약한 참조에 관한 것이었고 django 세션 미들웨어가 이것을 사용한다면? 약한 참고 문헌에 대한 기사 링크는 아래를 참조하십시오.
# Can't post over two links yet.
http://mindtrove.info/python-weak-references/
당신의 통찰력에 감사드립니다. 나는이 문제를 계속 전진 해 왔지만, 아직도이 문제가 발생하는 이유에 대해 궁금합니다.