我有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