我有python/django代碼託管在dotcloud和redhat openshift上。爲了處理不同的用戶,我使用令牌並將其保存在字典中。但是,當我從字典中獲得值時,它有時會引發錯誤(鍵值錯誤)。django/python:從字典中獲取值時出錯
import threading
thread_queue = {}
def download(request):
dl_val = request.POST["input1"]
client_token = str(request.POST["pagecookie"])
# save client token as keys and thread object as value in dictionary
thread_queue[client_token] = DownloadThread(dl_val,client_token)
thread_queue[client_token].start()
return render_to_response("progress.html",
{ "dl_val" : dl_val, "token" : client_token })
下面的代碼以1秒的間隔通過javascript xmlhttprequest執行到服務器。 它將檢查另一個線程中的變量並將值返回給用戶頁面。
def downloadProgress(request, token):
# sometimes i use this for check the content of dict
#resp = HttpResponse("thread_queue = "+str(thread_queue))
#return resp
prog, total = thread_queue[str(token)].getValue() # problematic line !
if prog == 0:
# prevent division by zero
return HttpResponse("0")
percent = float(prog)/float(total)
percent = round(percent*100, 2)
if percent >= 100:
try:
f_name = thread_queue[token].getFileName()[1]
except:
downloadProgress(request,token)
resp = HttpResponse('<a href="http://'+request.META['HTTP_HOST']+
'/dl/'+token+'/">'+f_name+'</a><br />')
return resp
else:
return HttpResponse(str(percent))
測試幾天後,它有時會返回:
thread_queue = {}
有時候成功:
thread_queue = {'wFVdMDF9a2qSQCAXi7za': , 'EVukb7QdNdDgCf2ZtVSw': , 'C7pkqYRvRadTfEce5j2b': , '2xPFhR6wm9bs9BEQNfdd': }
我,當我通過管理本地運行的Django從來沒有得到這樣的結果。 py runserver,並通過谷歌瀏覽器訪問它,但是當我將它上傳到dotcloud或openshift時,它總是會出現上述問題。 我的問題:
- 我該如何解決這個問題?
- dotcloud和openshift是否限制他們的python cpu使用?
- 或者是python字典裏面的問題?
謝謝。
謝謝,我會努力的Redis – Alvin 2012-08-17 17:57:09
在我的代碼,即時存儲線程對象字典。我可以擁有與redis相同的內容來存儲我的線程對象嗎? – Alvin 2012-08-17 18:17:19
@alvin,如果你的線程對象可以被醃製,你可以醃製它,並將它存儲在redis中,就像你將它存儲在本地字典中一樣,你的密鑰將是你今天使用的相同標記,並且值將是你的pickled目的。當你想要的時候,你可以把它拉出來,取出它,然後你就可以像以前一樣使用它。 – 2012-08-17 21:02:39